Basic HTML version of Foils prepared 13 October 97

Foil 40 Keyboard and Mouse Events

From Java Tutorial, July 1, 1996 CSC499(JSU) CPS406/606 -- Fall Semester 97. by Nancy J. McCracken,Geoffrey C. Fox

API for class KeyEvents
More generally, any component, including containers, can generate mouse and keyboard events as the user moves or clicks the mouse in the window, or types a single key on the keyboard.
This is most likely used in a Canvas or graphics drawing area.
Typing a single key generates two 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)
  • this is called for an action key, such as arrow keys, home, etc.
  • public void keyTyped (KeyEvent e)
  • this is called for other keys, such as text characters
  • public void keyReleased (KeyEvent e)
  • this is called when any key is released
Typically, one uses methods to find the name of the key pressed or typed:
String s = e.getKeyText (e.getKeyCode());



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Wed Apr 1 1998