Java会忽略机器人生成的VK_Enter事件 [英] Java ignores VK_Enter event generated by robot

查看:155
本文介绍了Java会忽略机器人生成的VK_Enter事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用机器人编写一些集成测试.我让机器人打开了一个菜单,它应该从菜单中选择一个选项.除了回车键似乎被忽略.菜单将打开,并且选择了正确的菜单项,但是如果我按Enter键,则没有采取任何措施.如果我手动按适当的键,则可以完成预期的操作.如果我在非Java应用程序上运行机器人,则其他应用程序将正确响应enter事件.因此,我已经验证我可以正确发送Enter事件,并且Java菜单应该对此做出响应;但有些东西行不通.

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.

我还尝试用空格事件替换enter事件(菜单也应响应),但也没有响应.

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

我正在使用最新的Sun JDK在redhat linux上运行.我很确定这不是我的代码的明显问题,因为我发现这篇文章描述了遇到完全相同的问题的人,但是它没有解决方案:

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);
}

推荐答案

我终于解决了这个问题.我让我的机器人代码与按钮效果一起在事件分发线程中运行.这意味着在我的机器人完成执行之前,所有按钮都无法响应我的机器人的动作.

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.

我不确定为什么在这种情况下这会阻止我的机器人正常工作.我发现,如果删除了ctrl-F4命令,机器人将对enter命令做出响应,但是似乎在机器人返回时,这两个命令都应该已经排队并正确执行了.我假设排队的事件数没有限制,或者我以某种方式最终在两个事件之间进行了数据争夺.无论哪种方式,通过将机器人移动到单独的线程中,我都可以达到预期的行为.

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.

我无法创建SSCCE的原因是因为我试图将机器人合并到按钮中,并且试图让机器人一次执行多个事件.但我没有尝试按下按钮来产生一个机器人执行多个事件.要重现此问题,我最终会得到与原始代码一样复杂的代码.

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.

这篇关于Java会忽略机器人生成的VK_Enter事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆