Skip to Main Content

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

865453May 30 2011 — edited Jun 14 2011
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Jul 12 2011
Added on May 30 2011
2 comments
7,390 views