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

Robot is not firing the correct keycode being passed in.

XMLWordPrintable

    • Fix Understood
    • sparc
    • solaris_8

      I found this bug indirectly while running a Swing test. The problem is that the virtual key which is fed into Robot's keyPress/keyRelease is not the one fired into the event queue. In particular, VK_DELETE for Solaris is returning 110 for a keycode when it should be 127. I decided to create a small program to check the other keycodes and I surprisingly found that there are others which are not mapped correctly.

      For the VK_DELETE problem, it seemed to have been there since the beginning, at least I tried it on JDK 1.3.1.

      I ran the following program on:

      java version "1.4.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b86)
      Java HotSpot(TM) Client VM (build 1.4.0-rc-b86, mixed mode)

      When you run the following program if you see a "-1" for the fired value, it means that no event was fired. If it's a "-2", that means some kind of exception was thrown (i.e. IllegalArgumentException). I have spoken with David Herron regarding some of this and I realized that not all the VK entries are applicaple to Robot, so instead of making assumptions about what should and shouldn't be fired, I'll let the developers decide on this. However, I know for sure that VK_DELETE should at least be fixed. The following program will only print out the values which aren't mapped to a value.

      ---------------------------------- Cut Here ----------------------------------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.lang.reflect.*;

      public class VK_Test extends JFrame {

          private JPanel panel;
          private JTextField textField;
          private Robot robot;
          private int value = -1;
          private Class theClass;

          public static void main(String[] args) {
              VK_Test r = new VK_Test();
              r.runTest();
          }

          public VK_Test() {
              try {
                  robot = new Robot();
              } catch(Exception e) {
                  e.printStackTrace();
              }

              textField = new JTextField(10);
              textField.addKeyListener(new KeyListener() {
                  public void keyPressed(KeyEvent e) {
                      value = e.getKeyCode();
                  }

                  public void keyReleased(KeyEvent e) {}
                  public void keyTyped(KeyEvent e) {}
              });

              panel = new JPanel();
              panel.add(textField);

              getContentPane().add(panel);

              pack();
              setVisible(true);
          }

          public void runTest() {
              try {
                  Thread.sleep(2000);
              } catch(Exception e) {
                  e.printStackTrace();
              }

              textField.requestFocus();

              try {
                  theClass = Class.forName("java.awt.event.KeyEvent");
              } catch(Exception e) {
                  e.printStackTrace();
              }

              Field[] fields = theClass.getFields();
              int integer = 0;
              
              for(int i = 0; i < fields.length; i++) {
                  String name = fields[i].getName();

                  if(name.indexOf("VK") != -1) {
                      try {
                          integer = fields[i].getInt(fields[i]);
                      } catch(Exception e) {
                          e.printStackTrace();
                      }


                      try {
                          robot.keyPress(integer);
                          robot.keyRelease(integer);
                      } catch(Exception e) {
                          value = -2;
                      }

                      try {
                          Thread.sleep(500);
                      } catch(Exception e) {
                          e.printStackTrace();
                      }

                      if(value != integer) {
                          System.out.println(name + " - " + "Actual: " + integer + " Fired: " + value);
                      }

                      value = -1;
                  }
              }
          }
      }
      ---------------------------------- Cut Here ----------------------------------

      ###@###.### 2001-11-20
      Swing Test Development

            denis Denis Fokin (Inactive)
            elousunw Edmund Lou (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: