Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

integration - Java ignores VK_Enter event generated by robot

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){
    if(testName==currentTest)
        return;
    //overwrite config file with new data
    currentTest=testName;
    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
    pressKey(KeyEvent.VK_DOWN);
    pressKey(KeyEvent.VK_DOWN);
    pressKey(KeyEvent.VK_DOWN);
    pressKey(KeyEvent.VK_SPACE);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

    // 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...