When implementing a KeyListener in my application I have the following code. I have added the KeyListener to a JTextField control using addKeyListener(this) and the current class implements KeyListener.

   public void keyPressed(KeyEvent ev) {
      System.out.println("Pressed: "+ev.getKeyCode());

   public void keyReleased(KeyEvent ev) {
      System.out.println("Released: "+ev.getKeyCode());

   public void keyTyped(KeyEvent ev) {
      System.out.println("Typed: "+ev.getKeyCode());

When I go about typing in this specific JTextField, the keyPressed and keyReleased methods print out the key code results as expected, but the keyTyped event prints out "Typed: 0" on every key.

Why is this?


6 Years
Discussion Span
Last Post by bops

I guess that the key code is 0
Try printing out ev and see if the toString() output has anything useful


For keyTyped events use getKeyChar(), not getKeyCode()
(For KEY_TYPED events, the keyCode is VK_UNDEFINED.)

If I would like to check to see if the enter key has been typed, or perhaps the backspace key. What would I do then?

if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
   // This is how I would have done it...

Without being able to check the key code, it makes a it a little different.

Any idea on why this is the case? why not be able to call getKeyCode? Maybe Sun just enjoys being a little awkward.

Thank you.


Have you tried printing out the event and see if its toString() output has anything useful?


No, Sun isn't being difficult. Because of shift keys (etc) typing one character may involve multiple keys being pressed, thus mutliple keyCodes for one keyChar.
This is from the API doc:

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.

In more detail:

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.

The easiest way to proceed is to print the keyChar in your keyTyped handler and see exactly what char is generated. Backspace will be \b and enter will probably be \n (or maybe \r)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.