Sun Java Solaris Communities My SDN Account Join SDN
 
Code sample

Code Samples from the Java Developers Almanac 2000

 

Code Samples Index

These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms

Handling Key Presses

You can get the key that was pressed either as a key character (which is a Unicode character) or as a key code (a special value representing a particular key on the keyboard).

 component.addKeyListener(new MyKeyListener());
     
 public class MyKeyListener extends KeyAdapter {
   public void keyPressed(KeyEvent evt) {
     // Check for key characters.
     if (evt.getKeyChar() == 'a') {
       process(evt.getKeyChar());
     }
     
    // Check for key codes.
    if (evt.getKeyCode() == KeyEvent.VK_HOME) {
      process(evt.getKeyCode());
    }
   }
 }
 

Examplets provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon