Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4138880

JDK1.1.6: KeyEvent.getKeyChar returns wrong code

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.1.6
    • client-libs
    • x86, sparc
    • solaris_2.5, solaris_2.5.1



      Name: moC74494 Date: 05/15/98


      We have encountered a bug when using KL Group's JCComboBox on Solaris
      with JDK1.1.6. When we attempt to do a backspace, a little rectangle is
      displayed and the character is not erased. I have included a test
      program which reproduces this bug. The test program displays a
      java.awt.TextField and a jclass.bwt.JCComboBox control and will display
      both the keyChar and the keyCode that we receive for both controls.
      With the JCComboBox we receive a keyCode of 8, but a keyChar of 65288
      (or 0xFF08). In addition, any Capital letter (SHIFT+#) whill not be
      displayed and will have 0xff00 in the upper byte.

      I have tracked down this problem to changes that have been made in
      java.awt.Component.java in jdk1.1.6 in dispatchEventImpl
              if (areInputMethodsEnabled()
                   && (
                       // Otherwise, we only pass on low-level events, because

                       // a) input methods shouldn't know about semantic
      events
                       // b) passing on the events takes time
                       // c) isConsumed() is always true for semantic events.
                       // We exclude paint events since they may be numerous
      and shouldn't matter.
                       (e instanceof ComponentEvent) && !(e instanceof
      PaintEvent))) {
                          InputContext inputContext = getInputContext();
                          if (inputContext != null) {
                              inputContext.dispatchEvent(e);
                              if (e.isConsumed()) {
                                   return;
                               }
                           }
              }

      The "offending" line is
              InputContext inputContext = getInputContext();
      If this line is removed I no longer see the bug.

      Now, I believe that this is a problem with Sun's Code and not the KL
      Group's code (or at least Sun is partially responsible because the Code
      to deal with TextField's etc, never reaches the offending line. If we
      were to change
          if (areInputMethodsEnabled() &&
      to
          if (true

      then Sun's TextField control will also get a keyChar of 65288

      The reason for this is that the java.awt.TextField has the
      areInputMethodsEnabled overriden in java.awt.TextComponent. The
      areInputMethodsEnabled is new to jdk1.1.6 and is not in jdk1.1.5

      I have chased the problem down to getting a bad value from
              java.awt.EventQueue.postEvent
              sun.awt.motif.MToolkit.postEvent
              sun.awt.motif.MComponentPeer.postEvent

      It certainly seems that the problem is buried somewhere in the motif
      peer stuff.

      My solution to the problem is a bit lame, but it seems to work and I
      suspect that it is unlikely to cause any adverse affects. I go all the
      way back to
          jclass.base.TransientComponent
      which is the first place that I can actually get hold of an KeyEvent
      object and not an Event Object and in processKeyEvent I do
          protected void processKeyEvent(KeyEvent keyEvent1) {
              int int2= keyEvent1.getID();
             if ((int)(keyEvent1.getKeyChar()) == 65288){ // Have bad
      character
                  // if the virtual key code says it's a backspace set it to
      be the right character
                  if (keyEvent1.getKeyCode() == KeyEvent.VK_BACK_SPACE){
                      keyEvent1.setKeyChar('\b');
                  }
              }

      The program that will reproduce this is


      import java.awt.*;
      import java.awt.event.*;
      import jclass.bwt.JCComboBox;
      import jclass.bwt.JCComboBoxListener;

      public class BackSpaceTest extends Panel {
          MyTextField textField;
          MyJCComboBox combo;
          public BackSpaceTest(){
              textField = new MyTextField(15);
              //textField.addKeyListener(new KeyHandler());
              add(textField);
              combo = new MyJCComboBox();
              combo.setStyle(JCComboBox.COMBOBOX_DROPDOWN);
              //combo.addKeyListener(new KeyHandler());
              add(combo);
          }
          public class MyTextField extends TextField {
              public MyTextField(int i){
                     super(i);
              }
              public boolean keyDown(
                  Event event1,
                  int int2)
              {
                  System.out.println("Got keyDown event " + event1.key);
                  return false;
              }
          }
          public class MyJCComboBox extends JCComboBox {
              public MyJCComboBox(){
                     super();
              }
              public boolean keyDown(
                  Event event1,
                  int int2)
              {
                  System.out.println("Got keyDown event " + event1.key);
                  System.out.println("Got keyDown key " + int2);
                  return false;
              }
          }
          public class KeyHandler implements KeyListener {
              public void keyPressed(KeyEvent e){
                  System.out.println("Got KeyPressed");
                  System.out.println("KeyChar is " + e.getKeyChar());
                  System.out.println("KeyCode is " + e.getKeyCode());
                  System.out.println("KeyCode text is " + e.getKeyText(e.getKeyCode()));
              }
              public void keyTyped(KeyEvent e){
              }
              public void keyReleased(KeyEvent e){
              }
          }
          
          public static void main(String []args){
              Frame frame = new Frame("BackSpaceTest");
              frame.setSize(300,250);
              BackSpaceTest bst = new BackSpaceTest();
              frame.setLayout(new GridLayout(1,1));
              frame.add(bst);
              frame.addWindowListener(new WindowAdapter(){
                  public void windowClosing(WindowEvent e){
                      System.exit(0);
                  }
              });
              frame.setVisible(true);
          }
      }
      (Review ID: 29668)
      ======================================================================



      sandhya.vora@Eng 1998-10-29

      Combo box items do not select on upper case keystrokes.

      Upper Case keystrokes do not select combo box items on Solaris reference implementation 1.1.6/1.1.7, as well as on Optimized 1.1.6. They DO on
      WinNT. Also overriding the DefaultKeySelectionManager to ensure that upper case should work still does not work on Solaris. The problem appears to be in the
      keyboard event handling on Solaris.

      Compile and run the .java (ComboTest.java, WindozeLookAndFeel.java) files
      as below, and you will see the following:

      Upper Case keystrokes do not select combo box items on Solaris. To reproduce it,

      Tab until you go into the second column, 1st row. (DO not use mouse)
      Type either Upper Case A, B or C, it will not select the entry. Lower case a,b, c will just work fine.

      ComboTest.java >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

      import com.sun.java.swing.*;
      import com.sun.java.swing.event.*;

      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;

      public class ComboTest extends JPanel {

        protected String[] choices = {"Aaaaaaa", "Bbbbbbb", "Ccccccc"};

        public ComboTest() {
          super();
          setLayout(new GridLayout(1, 3, 5, 5));
          add(getMetalCombos());
          add(getWindozeCombos());
          add(getMotifCombos());
        }

        protected JPanel getMetalCombos() {
          return getCombos("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
        }

        protected JPanel getWindozeCombos() {
          return getCombos("WindozeLookAndFeel");
        }

        protected JPanel getMotifCombos() {
          return getCombos("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        }

        protected JPanel getCombos(String lf) {
          setLF(lf);
          JPanel p = new JPanel();
          p.setLayout(new GridLayout(3, 1, 5, 5));
          p.add(getCombo1(lf));
          p.add(getCombo2(lf));
          p.add(getCombo3(lf));
          return p;
        }

        protected void setLF(String lf) {
          try {
            UIManager.setLookAndFeel(lf);
          }
          catch (Exception e) {
            System.out.println(e);
          }
        }

        //
        // vanilla JComboBox
        //
        protected JComboBox getCombo1(String tp) {
          JComboBox c = new JComboBox(choices);
          c.setToolTipText(tp);
          return c;
        }

        //
        // vanilla JComboBox with a repaint thrown in when an item is selected
        //
        protected JComboBox getCombo2(String tp) {
          JComboBox c = new JComboBox(choices);
          c.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
      repaint();
            }
          } );
          c.setToolTipText(tp);
          return c;
        }

        //
        // JComboBox with custom key selection manager
        //
        protected JComboBox getCombo3(String tp) {
          JComboBox c = new JComboBox(choices);
          c.setKeySelectionManager(new JComboBox.KeySelectionManager() {
            public int selectionForKey(char key, ComboBoxModel model) {
      int selected = -1;
              if (key == 'A' || key =='a') {
      selected = 0;
      }
              else if (key == 'B' || key =='b') {
      selected = 1;
      }
              else if (key == 'C' || key =='c') {
      selected = 2;
      }
      repaint();
      return selected;
            }
          } );
          c.setToolTipText(tp);
          return c;
        }

        public final static void main(String[] args) {

          JFrame f = new JFrame();
          ComboTest t = new ComboTest();
          f.getContentPane().add(t);
          f.pack();
          f.show();
        }

      }
          

      WindozeLookAndFeel.java >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

      public class WindozeLookAndFeel extends com.sun.java.swing.plaf.windows.WindowsLookAndFeel {

        public String getName() {
          return "Windoze";
        }
        
        public String getID() {
          return "Windoze";
        }
        
        public String getDescription() {
          return "Windoze";
        }
        
        public boolean isNativeLookAndFeel(){
          return false;
        }
        
        public boolean isSupportedLookAndFeel() {
          return true;
        }

      }

            okutsu Masayoshi Okutsu
            moanceasunw Mircea Oancea (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: