Key Events
Typing a single key generates KeyEvents. These events must be handled by implementing the KeyListener interface. It has three methods corresponding to the three actions that can occur on a key:
- public void keyPressed (KeyEvent e)
- public void keyReleased (KeyEvent e)
- these methods are called when a key is pressed down and released up, respectively, and report a virtual key code, which is an int encoding of the keys, such as VK_SHIFT, VK_A, . . .
- public void keyTyped (KeyEvent e)
- this reports the character on the key that was pressed
Typically, one uses methods on the key event to find the name of the key or key code:
- String s = e.getKeyChar ( );
- String t = e.getKeyText (e.getKeyCode());