Java awt机器人仍然无法按下Windows上的非小键盘箭头?

时间:2014-02-05 14:04:02

标签: java awt keypress awtrobot arrow-keys

这个错误已知多年,但仍然存在于我在Windows 8上使用的Java 1.7.0_25版本中。无论是否打开了numlock,以下结果都是相同的:

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

那么,是否有解决方案?

2 个答案:

答案 0 :(得分:0)

//按下WINDOWS + ARROW LEFT

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

适合我的工作:)

答案 1 :(得分:0)

可能的解决方法是禁用numlock。见this jdk bug comment