Take the 2-minute tour ×

I'm writing some integration tests using a robot. I have the robot opening a menu and it should be selecting one of the options form the menu; except the enter key seems to be ignored. The menu will open and the correct menu item is selected, but no action has been taken as it should if I hit enter. If I manually press the appropriate keys it does what is expected. If I run the robot on a non-java application the other application will respond to the enter event correctly. So I've verified that I'm sending an enter event correctly and that java menu should respond to it; but something isn't working.

I've also tried replacing the enter event with a space event (with the menu should also respond to) and got no response either.

I'm running on redhat linux with the latest sun JDK. I'm pretty sure it's not an obvious problem with my code since I've found this post describing someone who ran into the exact same problem, but it has no solution: http://www.velocityreviews.com/forums/t666100-robot-and-awt-on-linux.html

private void requestTest(String testName){
    //overwrite config file with new data
    overwriteFile(configFile, getCurrentConfigFile());

    //close current graph
    pressKeyCombo(KeyEvent.VK_CONTROL, KeyEvent.VK_F4);

    //open File menu
    pressKeyCombo(KeyEvent.VK_ALT, KeyEvent.VK_F);
    //select the 'load defaults' option
share|improve this question
Maybe its a platform issue, maybe its a code issue. Post you SSCCE (sscce.org) that demonstrates the problem. –  camickr Jul 8 '11 at 20:44
That is NOT a SSCCE. Please explain to me how that is compilable? –  camickr Jul 8 '11 at 20:54
this is all I have, I have not been able to recreate the issue in small segments of code –  drew Jul 8 '11 at 21:13
Exactly. That is the point of creating a SSCCE. Now you know the problem is in your code and NOT a JDK problem. Now that you now the basic code works it is up to you to figure out what is different between the SSCCE and your real code. When can't guess what your real code looks like in the context of your entire app. –  camickr Jul 9 '11 at 2:13
add comment

4 Answers

I guess that your problem is that you are using VK_DOWN only while the event is triggered when key is UP. I mean that you should simulate key typing event, i.e. DOWN and then UP. Try it, I hope this will work.

share|improve this answer
actually I'm afraid that is just an issue with my posting only a segment of my code. the press key and press key combo methods press and release the buttons, as well as putting a brief pause between key strokes –  drew Jul 8 '11 at 21:14
add comment
up vote 0 down vote accepted

I finally solved this. I had my robot code running in the event-dispatching thread along with the button effects. This meant that none of the buttons could respond to my robot's action until my robot completed execution.

I'm not entirely certain why this would prevent my robot from working correctly in this case. I discovered that if I removed the ctrl-F4 command that the robot would respond to the enter command, but it seems as if both commands should have been queued and executed correctly the moment the robot returned. I assume either there is a limit to the number of events queued or that I somehow ended up with a datarace between the two events. Either way by moving my robot into a separate thread I get the desired behavior.

incidentally the reason I couldn't create a SSCCE was because I tried to incorporate the robot into a button and I tried having the robot do multiple events at once; but I didn't try a button spawning a robot doing multiple events. To recreate this issue I would have ended up with code just as complex as my original code.

share|improve this answer
add comment

Well, I got stuck on the same issue of pressing the Enter key. Me too, have no explanation when I tried to operate a native OS File Chooser dialogue using Enter in the end and it did not work. But it seems, that the issue can be solved by creating another Robot object where you call the Enter event. So please let me at least assist you or other lone surfers coming across to seek counsel. ;-)

public static void enterKeyIssueTest() {

    // both lines put some content to the Clipboard
    StringSelection ss = new StringSelection("/fancyUser/tightDir/coolFile.apk");

    // 1st Robot Object
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);   // press Ctrl
    robot.keyPress(KeyEvent.VK_V);         // and press V
    robot.keyRelease(KeyEvent.VK_V);        // release Ctrl
    robot.keyRelease(KeyEvent.VK_CONTROL);  // release V

    // 2nd Robot to my avail
    Robot okRobot = new Robot();

    // presses Enter
    okRobot.keyPress(KeyEvent.VK_ENTER);    // press Enter
    okRobot.keyRelease(KeyEvent.VK_ENTER);  // release Enter

This is indeed a very small example, but I hope the explanations in the code above will assist. Indeed, the second Robot object did the Enter event for me.

Best regards, Semo

share|improve this answer
add comment

Same'o, Same'o here.

I am automating a java application using selenium (WebDriver) and the application is using File Uploads all over the place.

Those are implemented using a (Windows 7, 32 bits) file browsing dialog. Of course, that file browsing dialog is out of reach for WebDriver and i had to device a way to enter the file name.

As suggested in this post, i used a Robot instance. This works fine for everything EXCEPT the section equivalent to:

// presses Enter
okRobot.keyPress(KeyEvent.VK_ENTER);    // press Enter
okRobot.keyRelease(KeyEvent.VK_ENTER);  // release Enter

The file dialog behaves as though the last two statements were missing (and i tried a whole bunch of combinations and permutations of getting it going.)

share|improve this answer
add comment

Your Answer


