2014-02-05 34 views
0

此错误认识多年,但目前仍处于目前在Java中1.7.0_25版本我使用的是Windows 8的下面的结果这是一样的,不管阉我有NumLock键开启或不:Java awt机器人仍然不能按窗口上的非数字键箭头?

Robot bot = new Robot(); 

bot.keyPress(KeyEvent.VK_UP); //this in documentation is non-numpad up arrow key 
bot.keyRelease(KeyEvent.VK_UP); //pressed the numpad up arrow key 

//folowing line is line #43 
bot.keyPress(KeyEvent.VK_KP_UP); //this in documentation is numpad up arrow key 
bot.keyRelease(KeyEvent.VK_KP_UP); //causes folowing exception: 

Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code 
at sun.awt.windows.WRobotPeer.keyPress(Native Method) 
at java.awt.Robot.keyPress(Robot.java:358) 
at test.RobotArrow.main(RobotArrow.java:43) 

我知道这个问题已经问了here但是一年前,那么有没有进展?我不能谷歌什么,甚至有一个ofiicial bug report

那么,有没有finnaly解决方案或不?

回答

0

//按Windows +左箭头

Robot divideWindow = new Robot(); 
divideWindow.keyPress(KeyEvent.VK_WINDOWS); 
divideWindow.delay(100); 
divideWindow.keyPress(KeyEvent.VK_LEFT); 
divideWindow.delay(100); 
divideWindow.keyRelease(KeyEvent.VK_LEFT); 
divideWindow.delay(100); 
divideWindow.keyRelease(KeyEvent.VK_WINDOWS); 

工作正常,我:)

+0

我测试这与空JFrame和捕获与KeyboardFocusManager事件,这不会做我想要的。我想通过机器人按下非数字键盘箭头键,这会按下窗口+箭头键,因此我的JFrame一致地移动,我不想那样。 – kajacx

+0

这不能代替按箭头键;它会激活Windows快捷方式来移动窗口。 – ZX9