Java: KeyReleased(), keyPressed() vs. keyTyped()

This blog article talks about the differences among three methods in Java: keyReleased(), keyPressed() and keyTyped(). Updated once in Nov. 17, 2013. Any comments are welcomed.

According to an answer on StackOverFLow[1], the differences are:

  • keyPressed(): when the key goes down
  • keyReleased(): when the key comes up
  • keyTyped(): when the unicode character represented by this key is sent by the keyboard to system input.

When it comes to the real application, it means that:

  • If you want to get a immediate response, whether or not a character, use keyPressed().
  • If you want to get a single command, whether or not a character, you’d better use keyReleased().
  • If you want to input only a character and hit again and again while holding the key, then you use keyTyped().

However, I still have one question: Can I use keyTyped() with an unprinted key, such as using the Backspace to clear the text field?

The result occured after I read about an authoritative and detailed description given by the Java website officially. Please read them below if interested.

KeyEvent: An event which indicates that a keystroke occurred in a component.

This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed. The event is passed to every KeyListener orKeyAdapter object which registered to receive such events using the component’s addKeyListener method. (KeyAdapter objects implement the KeyListener interface.) Each such listener object gets this KeyEvent when the event occurs.

“Key typed” events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input. In the simplest case, a key typed event is produced by a single key press (e.g., ‘a’). Often, however, characters are produced by series of key presses (e.g., ‘shift’ + ‘a’), and the mapping from key pressed events to key typed events may be many-to-one or many-to-many. Key releases are not usually necessary to generate a key typed event, but there are some cases where the key typed event is not generated until a key is released (e.g., entering ASCII sequences via the Alt-Numpad method in Windows). No key typed events are generated for keys that don’t generate Unicode characters (e.g., action keys, modifier keys, etc.). The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED. For key pressed and key released events, the getKeyCode method returns the event’s keyCode. For key typed events, the getKeyCode method always returns VK_UNDEFINED.

“Key pressed” and “key released” events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don’t generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the getKeyCode method, which returns a virtual key code.

Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as “A”, which comes from shift and “a”).

For example, pressing the Shift key will cause a KEY_PRESSED event with a VK_SHIFT keyCode, while pressing the ‘a’ key will result in a VK_A keyCode. After the ‘a’ key is released, a KEY_RELEASED event will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar value of ‘A’ is generated.

References

  1. java – KeyListener, keyPressed versus keyTyped – Stack Overflow
  2. Class KeyEvent Instructions In Java SE Documents

本作品使用基于以下许可授权:Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

发表评论

电子邮件地址不会被公开。 必填项已用*标注