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

getKeyCode(), getKeyChar() returns incorrect value at KEY_PRESSED event.

XMLWordPrintable

    • 1.2fcs
    • generic
    • generic
    • Not verified

      Run the attached program and operate.
      When pressed some keys, getKeyCode() and getKeyChar() returns incorrect value.

      - Windows

          in case of '-', '\(back slash)' :

            Result : keyCode=0 & keyChar=' '
            --------------------------------------------------------------------------------
            java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyChar=' '] on textfield0:
                   Unknown keyCode: 0x0/
            java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyChar='-'] on textfield0: -
            java.awt.event.KeyEvent[KEY_RELEASED,keyCode=0,keyChar=' '] on textfield0
            --------------------------------------------------------------------------------
            
          in case of '\(yen sign)', '[', ']', ';', ':', '/', '.', ',' :

            Result : keyChar=' '
            --------------------------------------------------------------------------------
            java.awt.event.KeyEvent[KEY_PRESSED,keyCode=92,keyChar=' '] on textfield0: \/
            java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyChar='\'] on textfield0: -
            java.awt.event.KeyEvent[KEY_RELEASED,keyCode=92,keyChar=' '] on textfield0
            --------------------------------------------------------------------------------

      - Solaris

          in case of '-' : keyCode=0

            Result:
            --------------------------------------------------------------------------------
            java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyChar='-'] on textfield0:
                   Unknown keyCode: 0x0/ -
            java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyChar='-'] on textfield0: -
            java.awt.event.KeyEvent[KEY_RELEASED,keyCode=0,keyChar='-'] on textfield0
            --------------------------------------------------------------------------------

      When these operate with shift key, result are the same.

      <reference>
          JDK1.2beta3 : getKeyCode() returns incorrect value, but getKeyChar() returns
                        correct value.
          JDK1.1.6 : all of keys retruns available KeyCode and correct KeyChar.
                        This is inconvenient for the application that treats also function key,
                        because the application has no means to know that next KEY_TYPED
                        has charcode, when the application received KEY_PRESSED.

      Program:
      -------------------------------------------------------------------------------------
      import java.applet.*;
      import java.awt.*;
      import java.awt.event.*;

      public class KeyEvents extends Applet implements KeyListener, MouseListener
      {
          TextArea area;
          TextField field;
          Canvas canvas;
      public KeyEvents()
      {
              this.area = new TextArea(10,40);
              this.field = new TextField(20);
              this.canvas = new Canvas();
      this.canvas.setSize(320,80);
      this.canvas.setBackground(Color.lightGray);
      this.add(field);
      this.add(canvas);
      this.add(area);
      this.canvas.addKeyListener(this);
      this.canvas.addMouseListener(this);
      this.field.addKeyListener(this);
      this.field.addMouseListener(this);
      }
      public void init()
      {
      }
      public void keyPressed(java.awt.event.KeyEvent event)
      {
      String message;
              message=""+event+": "+ event.getKeyText(event.getKeyCode()) + "/ "
              + event.getKeyChar();
      System.out.println(message);
      this.area.append(message+" \n");
      }
      public void keyReleased(java.awt.event.KeyEvent event)
      {
      String message;
      message=""+event;
      System.out.println(message);
      this.area.append(message+" \n");
      }
      public void keyTyped(java.awt.event.KeyEvent event)
      {
      String message;
      message=""+event;
      System.out.println(message+": "+event.getKeyChar());
      this.area.append(message+" \n");
      }
      public void mouseClicked(java.awt.event.MouseEvent event)
      {
      }
      public void mouseEntered(java.awt.event.MouseEvent event)
      {
      }
      public void mouseExited(java.awt.event.MouseEvent event)
      {
      }
      public void mousePressed(java.awt.event.MouseEvent event)
      {
      this.canvas.requestFocus();
      }
      public void mouseReleased(java.awt.event.MouseEvent event)
      {
      }

          public static void main(String[] args) {
              KeyEvents obj = new KeyEvents();
              Frame f = new Frame("KeyEvent Test");
              f.add(obj);
              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {System.exit(0);}
              });
              f.pack();
              f.show();
          }
      }
      -------------------------------------------------------------------------------------

            duke J. Duke
            eishidasunw Etsuko Ishida (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: