The information in this article applies to:
Programs that try to respond to KEY_PRESSED events miss those events for certain keys and certain controls that use the 1.1 or 1.02 event model of the Java Development Kit (JDK) from Sun Microsystems Inc. For example, this problem occurs when trying to intercept a TAB inside a TextArea.
Certain controls do not generate KEY_PRESSED or ACTION_PRESSED events, or call the keyPressed method of a KeyListener, for certain keys.
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
import java.awt.*; // To see JDK 1.1-style events, uncomment this line. // import java.awt.event.*; public class Testevent { public static void main(String args[]) { Frame f=new Frame(); f.setLayout(new java.awt.BorderLayout()); TextArea ta = new TextAreaEvents(); f.add("Center", ta); f.setSize(320,200); f.setVisible(true); // To see JDK 1.1-style events, uncomment this line. // To see JDK 1.02-style events, leave it out. // ta.addKeyListener(new TestKeyListener()); } } class TextAreaEvents extends TextArea { public boolean handleEvent(Event e) { System.out.println(e); System.out.print(e.id==Event.KEY_PRESS? "KEY_PRESS":""); System.out.print(e.id==Event.KEY_RELEASE? "KEY_RELEASE":""); System.out.print(e.id==Event.KEY_ACTION? "KEY_ACTION":""); System.out.print(e.id==Event.KEY_ACTION_RELEASE? "KEY_ACTION_RELEASE":""); System.out.println(); return super.handleEvent(e); } } // To see JDK 1.1-style events uncomment the following class: /* class TestKeyListener implements KeyListener { public void keyPressed(KeyEvent e) { System.out.println("Listener -- pressed: "+e); } public void keyTyped(KeyEvent e) { System.out.println("Listener -- typed: "+e); } public void keyReleased(KeyEvent e) { System.out.println("Listener -- released: "+e); } } /* End of block. */
You will notice that the following controls do not create KEY_PRESSED or ACTION_PRESSED events, or call keyPressed, for the following keys:
Button | SPACE, ENTER |
Choice | SPACE, ENTER, HOME, END, PGUP, PGDOWN, ARROWUP, ARROWLEFT, ARROWRIGHT, ARROWDOWN |
Checkbox | SPACE, ENTER |
List | SPACE, ENTER, HOME, END, PGUP, PGDOWN, ARROWUP, ARROWLEFT, ARROWRIGHT, ARROWDOWN |
TextArea | TAB, PGUP, PGDOWN |
tab key-pressed event textarea Component