12.17 Key Event Handling
This section presents the KeyListener
interface for handling key events. Key events are generated when keys on the keyboard are pressed and released. A class that implements KeyListener
must provide declarations for methods keyPressed, keyReleased and keyTyped, each of which receives a KeyEvent
as its argument. Class KeyEvent
is a subclass of InputEvent
. Method keyPressed
is called in response to pressing any key. Method keyTyped
is called in response to pressing any key that is not an action key. (The action keys are any arrow key, Home, End, Page Up, Page Down, any function key, etc.) Method keyReleased
is called when the key is released after any keyPressed
or keyTyped
event.
The application of Figs. 12.36–12.37
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.