Java-JavaScript Codesharing/About key events

Aus Formel-Applet
Wechseln zu: Navigation, Suche

Key events in classica Java

To handle key events, you need to add a KeyListener to the object which receives the keystrokes.

In this example, a KeyManager object (classic version) is added to the senderTextArea.

A KeyListener has to implement three methods:

  • keyPressed (to get the keyCode, roughly speaking)
  • keyReleased (not used here)
  • keyTyped (to get the character, roughly speaking)

Key events in GWT

To handle key events, you need to add a KeyPressHandler and a KeyDownHandler to the object which receives the keystrokes. In this example, a KeyManager object (mobile version) implements both KeyPressHandler and KeyDownHandler, and is added to the sender TextBox.

  • A KeyPressHandler has to implement a onKeyPress method to handle a KeyPressEvent.
  • A KeyDownHandler has to implement a onKeyDown method to handle a KeyDownEvent.
  • There would be KeyUpHandlers too, which are not used by this example.

So there are different event classes with different methods, other than in classic Java. A mess, isn't it?

  • The onKeyDown method lets you get a keyCode, so it is rather like the classic keyPressed method.
  • The onKeyPress method lets you get a character, so it is rather like the classic keyTyped method.
  • Method onKeyPress (mobile) does not correspond to keyPressed (classic) - would be too simple - isn't it?

Different keyCodes

  • ENTER
    • mobile: keyCode=10
    • classic: keyCode=13
  • INSERT
    • mobile: keyCode=45
    • classic: keyCode=155
  • Ä problem (mobile): If you type ä, let ev be the keyPressEvent. ch=String.valueOf(ev.getCharCode()). Then "ä".equals(ch) is false, but "\u00e4".equals(ch) is true.
  • See source code for more quirks

Known bugs

  • µ results in MU (classic), but in CTRL-ALT-MU (mobile).
  • Same with € (EURO) and @ (AT)
  • ° results in DEGREE (classic), but in SHIFT-CARET (mobile).
  • Alt-+ results in ALT-PLUS (classic), but no event in mobile.