Java Glossary : KeyListener

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : K words : KeyListener.

KeyListener
If you want your component to process its own keystrokes, the easiest way is for your component to implement the KeyListener interface. This means you must write three routines, that the system calls each time the user hits a key. You always get a KeyEvent.KEY_PRESSED and KEY_RELEASED event, even for keys like Shift, Alt and function keys. You won't get a KEY_TYPED event when the user hits a key like Shift, Alt or a function key, but it may alter the effect of some other key that does generate a KEY_TYPED event.
public void keyPressed (KeyEvent e)
public void keyTyped (KeyEvent e)
public void keyReleased (KeyEvent e)
The various combinations of ID, keyCode() and keyChar() in the KeyEvent are quite complicated.

getID() tells you they type of event, basically which listener was used. KeyCode() gets you a raw Keyboard code e.g. "A". keyChar() gets you the cooked character e.g. "a".

I suggest downloading my little KeyPlay application. You can play with it, clicking the mouse and hitting keystrokes. A description of the events generated is dumped to the console. With it, you can quickly learn about the ordering of events, and the use of the fields.

Happily, keyPressed(KeyEvent e) only sees id=KeyEvent.KEY_PRESSED events, keyTyped only sees id=KEY_TYPED events, and keyReleased only sees id=KEY_RELEASED events.
Usually somewhere in the component or panel's constructor, you must remember to register your component as an interested listener with this.addKeyListener(this); Make sure you don't do it more than once or you will see multiple keyPressed events per keystroke. Your handler can use KeyEvent.consume() to mark the keystroke as fully handled. This will discourage default keystroke handling code from being invoked as well. Keep in mind your component won't see any keystroke events unless it has focus. Keep in mind that not all components generate KeyEvents. They may generate ActionEvents or ItemStateChangedEvents instead. See the essay under events for more details. (To come, techniques to control additional pre or post processing on a keystroke by the superclass.) See my essay on keystroke accelerators [an error occurred while processing this directive] on how I think user-configurable, globally-configurable, keystroke accelerators should be implemented. See my essay on JDK 1.1 events 2004-06-28.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 2455.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/keylistener.html J:\mindprod\jgloss\keylistener.html