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()); } } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.