This post show how to detect user keypress on browsers in Go. We will use arrow keys as example and show you how to detect arrow key presses on browsers in Go/GopherJS. The full code example of this post is on my GitHub.
We will attach an keyup event handler to the window object of current browser window/tab. When users press any key on the current browser window/tab, the event handler will run and we will check the keyCode property of the event. If the keyCode happens to be arrow keys, we will show which arrow key user press via div element. The div element is as follows:
<div id="info">Press any arrow key</div>
The above code in Go/GopherJS is as follows:
To make your code more readable, we can prettify the above code with godom:
|||[GopherJS] Keyboard Event - Arrow Keys Example|
|||[Golang] GopherJS DOM Example - Detect Keypress (Keyboard Event)|