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

Robot.keyPress/keyRelease generates wrong events for SHIFT+PG_DOWN and others

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • None
    • 5.0
    • client-libs
    • x86
    • windows_xp

      I was writing a test that required robot to generate a SHIFT+PG_DOWN, and it was impossible to make it work. This looks like a bug in Robot. Compile and run the test below. Press and then release SHIFT+PG_DOWN. You'll see the following correct output in the text area.

      java.awt.event.KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT]
      java.awt.event.KeyEvent[KEY_PRESSED,keyCode=34,keyText=Page Down,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_STANDARD]
      java.awt.event.KeyEvent[KEY_RELEASED,keyCode=34,keyText=Page Down,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_STANDARD]
      java.awt.event.KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT]

      Now hit the button at the bottom to have JRobot do the following sequence:

      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_PAGE_DOWN);
      robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
      robot.keyRelease(KeyEvent.VK_SHIFT);

      Instead of the correct events, the following are generated instead:

      java.awt.event.KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT]
      java.awt.event.KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT]
      java.awt.event.KeyEvent[KEY_PRESSED,keyCode=34,keyText=Page Down,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_NUMPAD]
      java.awt.event.KeyEvent[KEY_RELEASED,keyCode=34,keyText=Page Down,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_NUMPAD]
      java.awt.event.KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT]
      java.awt.event.KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT]

      That is, SHIFT is pressed/released, then PG_DOWN is pressed/released, then SHIFT is pressed/released again.

      Similar results occur for PG_UP, UP, and possibly others. Works fine though for characters, space bar, and others.

      Test case:
      -----------------------------------------------------

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class RobotTest extends JFrame {
          
          Robot robot;
          
          public RobotTest() {
              super("RobotTest");
              
              try {
                  robot = new Robot();
              } catch (Exception e) {
                  e.printStackTrace();
                  System.exit(1);
              }

              final JTextArea ta = new JTextArea();
              getContentPane().add(new JScrollPane(ta));
              
              AWTEventListener listr = new AWTEventListener() {
                  public void eventDispatched(AWTEvent event) {
                      ta.append(event.toString() + "\n");
                  }
              };
              
              Toolkit.getDefaultToolkit().addAWTEventListener(listr, AWTEvent.KEY_EVENT_MASK);

              JButton button = new JButton("Use Robot");
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ae) {
                      robot.keyPress(KeyEvent.VK_SHIFT);
                      robot.keyPress(KeyEvent.VK_PAGE_DOWN);
                      robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
                      robot.keyRelease(KeyEvent.VK_SHIFT);
                  }
              });
              
              getContentPane().add(button, BorderLayout.SOUTH);
          }

          private static void createAndShowGUI(String[] args) {
              RobotTest test = new RobotTest();
              test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test.setSize(600, 400);
              test.setLocationRelativeTo(null);
              test.setVisible(true);
          }
          
          public static void main(final String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      createAndShowGUI(args);
                  }
              });
          }
      }

            art Artem Ananiev (Inactive)
            shickeysunw Shannon Hickey (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: