2 Replies Latest reply: Jun 14, 2011 2:55 PM by DarrylBurke RSS

    java.awt.Robot: Shift key is not kept pressed while using keyPress method

    865453
      Hi,

      I have been trying to automate some tasks using FEST swing library, then I have been seen an unexpected behavior while trying to use the Shift key.
      While debugging, I realized that this behavior is also reproducible using java.awt.Robot class.

      Below, it is the code that I use.
          public void start(){
              try {
                  java.awt.Robot robot = new Robot();            
                  robot.delay(1000); 
                  robot.keyPress(KeyEvent.VK_TAB);
                  robot.keyRelease(KeyEvent.VK_TAB);
                  robot.keyPress(KeyEvent.VK_SHIFT);            
                  robot.keyPress(KeyEvent.VK_DOWN);
                  robot.keyRelease(KeyEvent.VK_DOWN);
                  robot.keyRelease(KeyEvent.VK_SHIFT);
              } catch (AWTException ex) {
                  Logger.getLogger(RobotKeyTest.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      The main class is a JFrame which contains a JTable on it, the JTable has a KeyListener to report the key events.
              table.addKeyListener(new KeyListener(){
                  @Override
                  public void keyTyped(KeyEvent e) {
                      System.out.println("Key "+ e.getKeyCode() +" typed");
                  }
                  @Override
                  public void keyPressed(KeyEvent e) {
                      System.out.println("Key "+ e.getKeyCode() +" pressed");
                  }
                  @Override
                  public void keyReleased(KeyEvent e) {
                      System.out.println("Key "+ e.getKeyCode() +" released");
                  }
              });        
      The intention is to select two rows in the table object, it is mandatory to use the keyboard for this task.
      Unfortunately the rows are not selected as expected and this is the output that I got from the listener:

      Key 9 pressed
      Key 0 typed
      Key 9 released
      Key 16 pressed
      Key 16 released
      Key 40 pressed
      Key 40 released
      Key 16 pressed
      Key 16 released

      AFAIK: 9 is VK_TAB, 40 is VK_DOWN and 16 is VK_SHIFT.
      What I am seeing is that when I invoke the keyPress method with VK_SHIFT as parameter, the key is pressed and released immediately, the same happens when invoking keyRelease method. This behavior is not seen using the other keys in the program (VK_DOWN and VK_TAB)

      Is there anything else that it could be missing here? It looks like an issue but I am not sure whether I am missing something else or not.
      This my setup:

      Java:
      Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
      Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)

      OS:
      Windows 7 64 bits, not reproducible on Mac 10.6.2

      Please let me know if further information is needed.

      Thanks in advance,
      Cristopherson
        • 1. Re: java.awt.Robot: Shift key is not kept pressed while using keyPress method
          865453
          OK, I realized that this is an old bug but fortunately there is a workaround, below are my findings in case someone else has been facing the same issue.

          1. There are already 2 bugs reported in Java bug database, since 2003 and 2006.
          1.1. [url http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4908075]Bug 4908075
          1.2. [url http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6463168]Bug 6463168
          2. There is a workaround mentioning to turn off the numlock keys should fix the issue. I wanted to do this in a programmatically way.
          3. I figured out that I could use this:
          Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false);
          4. The tip was found in this blog: [url http://weblogs.java.net/blog/joshy/archive/2003/08/swinghack_keybo.html]Keyboard spinner
          5. After making the proper changes, the shift key issue was fixed.
          6. Below is the final code:
          public void start(){
                  try {
                      java.awt.Robot robot = new Robot();            
                      robot.delay(1000); 
                      Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false);
                      robot.keyPress(KeyEvent.VK_TAB);
                      robot.keyRelease(KeyEvent.VK_TAB);
                      robot.keyPress(KeyEvent.VK_SHIFT);            
                      robot.keyPress(KeyEvent.VK_DOWN);
                      robot.keyRelease(KeyEvent.VK_DOWN);
                      robot.keyRelease(KeyEvent.VK_SHIFT);
                  } catch (AWTException ex) {
                      Logger.getLogger(RobotKeyTest.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
          Edited by: Cristopherson on Jun 14, 2011 9:10 AM
          Indexing was wrong