Microsoft SDK for Java

Q188808 BUG: KEY_PRESSED Event Not Generated for Some Controls

The information in this article applies to:

SYMPTOMS

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.

CAUSE

Certain controls do not generate KEY_PRESSED or ACTION_PRESSED events, or call the keyPressed method of a KeyListener, for certain keys.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a Java project and include the following class:
    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. */ 
  2. Build and execute the above Java class. You will notice that the TextArea control does not generate KEY_PRESSED events for the TAB key.

Other Affected Keys

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

Additional query words:

tab key-pressed event textarea Component

© 1999 Microsoft Corporation. All rights reserved. Terms of use.