BUG: java.awt.event.KeyListener.keyTyped Method Does Not Work

Article translations Article translations
Article ID: 190301 - View products that this article applies to.
This article was previously published under Q190301
Expand all | Collapse all

On This Page

SYMPTOMS

The methods java.awt.event.KeyEvent.setKeyChar and java.awt.event.KeyEvent.consume have no effect on the AWT control when called from within the java.awt.event.KeyListener.keyTyped method.

RESOLUTION

The workaround is to move the event handling from the keyTyped method to the keyPressed method.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a Java project and paste the following code into a file called "TestKeyTyped.java":
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TestKeyTyped extends Applet
    {
       TestPanel myTestPanel;
    
       public void init()
       {
          myTestPanel = new TestPanel();
          add(myTestPanel);
       }
    }
    
    class TestPanel extends Panel implements KeyListener
    {
       TextArea myTextArea;
    
       TestPanel()
       {
         setLayout(new BorderLayout());
         add("West", new Label("Lower case \'a\' should change to upper "
           + "case \'A\' and \'%\' should be consumed:"));
         myTextArea = new TextArea(1, 25);
         add("Center", myTextArea);
         myTextArea.addKeyListener(this);
       }
    
       public void keyTyped(KeyEvent e)
       {
    
          char ch = e.getKeyChar();
    
          if (ch == 'a')
          {
             e.setKeyChar('A');   // Bug: Has no effect within
                                  // keyTyped
          }
          else if (ch == '%')
          {
             e.consume();         // Bug: Has no effect
                                  // within keyTyped
          }
          else
          {
             e.setKeyChar(ch);    // Bug: Has no effect within
                                  // keyTyped
          }
          System.out.println("Modified event to be: "+e+
                              " Consumed:"+e.isConsumed());
        }
        public void keyPressed(KeyEvent e)
       {
          // WORKAROUND
          // Cutting the above code from the keyTyped method
          // and pasting it here will cause the setKeyChar and
          // consume calls to work.
      }
    
       public void keyReleased(KeyEvent e)
       {
       }
    }
    						
  2. Copy the following text into a file called "TestKeyTyped.html":
    <applet
    name="TestKeyTyped"
    code="TestKeyTyped"
    width="600"
    height="50"
    align="Top">
    </applet>
    						
  3. Build the project and execute it with the Internet Explorer browser.
The correct behavior when running this applet is for any "a" typed in the text area to be converted to "A" before being displayed and for any "%" typed in the text area to be consumed without being displayed on the screen. This does not work due to the bug mentioned above.

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:
http://www.microsoft.com/java

Properties

Article ID: 190301 - Last Review: June 14, 2006 - Revision: 4.0
APPLIES TO
  • Microsoft Software Development Kit for Java 2.02
  • Microsoft Software Development Kit for Java 2.01
  • Microsoft Software Development Kit for Java 2.02
  • Microsoft Software Development Kit for Java 3.0
  • Microsoft Software Development Kit for Java 3.1
  • Microsoft Software Development Kit for Java 3.2
  • Microsoft Internet Explorer 4.01 Service Pack 2
Keywords: 
kbbug kbnofix kbjava kbawtpkg kbcode KB190301

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com