Search code examples
javakeylistenerbackspace

Java KeyListener: Is the "Backspace" key pressed in KeyTyped


How do I tell if the user pressed the "Backspace" key from inside of the KeyTyped method? They value from arg0.getKeyChar() is not KeyEvent.CHAR_UNDEFINED, and arg0.getKeyCode() is KeyEvent.VK_UNDEFINED as it always will be inside of the KeyTyped method. Do I have to use the KeyReleased/KeyPressed methods and flag a boolean? Which method fires first? KeyReleased or KeyTyped?


Solution

  • It looks like KeyTyped cannot detect keys that don't have a visual representation. I assume this includes backspace.

    KeyListener, keyPressed versus keyTyped