O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using keyboard events

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.

How to do it...

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.

  1. To that end, we add the following event listener to main():
    document.onKeyPress.listen(_onKeyPress);
  2. The _onKeyPress method is as follows:
    _onKeyPress(KeyboardEvent e){
      if (e.keyCode == KeyCode.ENTER) submit(e);
    }

Now, pressing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required