June 2017
Intermediate to advanced
873 pages
18h 9m
English
Handling keyboard events in normal web applications is not so common. However, if you are writing a web game, you'll almost certainly want to catch arrow key input, and the like. This recipe shows us how we can handle those events.
Look at the keyboard project. The web page is a copy of the page used in the Preventing an onSubmit event recipe, but now submit stays enabled. Suppose we also want to ensure that pressing the Enter key provokes submit, just like clicking on the submit button.
main():document.onKeyPress.listen(_onKeyPress);
_onKeyPress method is as follows:_onKeyPress(KeyboardEvent e){
if (e.keyCode == KeyCode.ENTER) submit(e);
}Now, pressing ...
Read now
Unlock full access