Java ignores VK_Enter event generated by robot

做~自己de王妃 提交于 2019-12-01 14:21:48

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!