Is it possible to emit the numpad arrow key in Java?

128 Views Asked by At

Background Operating systems (I'm on Windows 10, if it matters) have a feature called Mouse Keys that allow you to perform the operations of the mouse using the numpad. Press 5 to click, 8/2/4/6 to move the cursor up/down/left/right, and so on.

The Desire I would like to move the cursor around when Mouse Keys is activated, programmatically, in Java. I specifically want to use keyboard events for this, as moving the mouse directly is verboten in the context I'm in.

The Problem I tried using Robot.keyPress and Robot.keyRelease with keycode KeyEvent.VK_KP_UP = 224, but that yields the following error:

java.lang.IllegalArgumentException: Invalid key code 
    at java.desktop/sun.awt.windows.WRobotPeer.keyPress(Native Method)
    at java.desktop/java.awt.Robot.keyPress(Robot.java:350)

And trying it with KeyEvent.VK_NUMPAD8 doesn't error out, but also doesn't make the mouse move.

I also tried jnativehook's GlobalScreen.postNativeEvent(new NativeKeyEvent(...)), and no amount of voodoo could get it to work either. There's no constant in NativeKeyEvent for the keypad up key, so that's not super surprising. When I try to send the keycode 224, the following error occurs:

com.github.kwhat.jnativehook.GlobalScreen postNativeEvent
WARNING: map_keyboard_event [97]: Unable to lookup scancode: 224

Am I asking the impossible?

0

There are 0 best solutions below