Simulate Enter Key Pressed in Go


Simulate enter key pressed via Go/ GopherJS, i.e., programmatically fire enter key event in browsers without user intervention.

JavaScript

From [2] we know JavaScript code for firing enter keyevent would be:

const ke = new KeyboardEvent("keyup", {keyCode: 13});
document.body.dispatchEvent(ke);

GopherJS

The above code in Go/GopherJS is as follows:

import (
      "github.com/gopherjs/gopherjs/js"
)

option := js.Global.Get("Object").New()
option.Set("keyCode", 13)
ke := js.Global.Get("KeyboardEvent").New("keyup", option)
js.Global.Get("document").Get("body").Call("dispatchEvent", ke)