0

This bug is known for years, yet is is still present in Java 1.7.0_25 version which I'm using on Windows 8. The following result are same regardless of wether i have numlock turned on or not:

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)

I know this question was already asked here but over a year ago, so is there any progress? I cant google anything, there is even an ofiicial bug report

So, is there finnaly a solution or not?

Community
  • 1
  • 1
kajacx
  • 12,361
  • 5
  • 43
  • 70

2 Answers2

0

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

Works fine for me :)

bhordupur
  • 9
  • 1
  • I'm testing this with empty JFrame and capturing event with KeyboardFocusManager and this doesn't do what I want. I want to press a non-numpad arrow key via Robot, this presses windows + arrow key, thusly moving my JFrame accordinly, i dont want that. – kajacx May 15 '14 at 16:52
  • This is not a substitute for pressing the arrow keys; it will activate Windows shortcuts to move windows. – ZX9 Apr 29 '16 at 16:40
0

A possible workaround is to disable numlock. See this jdk bug comment

Joe23
  • 5,683
  • 3
  • 25
  • 23