java中空格键vk_,Java会忽略机器人生成的VK_Enter事件

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

}

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值