I have in planning a Java program running on Windows that can display diffenrent macros on different runnig keys in the background. The problem is how to get Java to listen for the keys pressed when the application is not focused.
I found many opinions that this is impossible. But I also found this one written by Stefano here on SO. This solution, unfortunately, is not enough for me, at least there is no impotent information. The function MsgWaitForMultipleObjects()returns a single value if the key is not pressed ... this is normal. After pressing the key, it returns a different value ... this will be normal if the function does not return the same value as after the keypress event.
Here is the flow test:
public class KeyListener extends Thread {
public KeyListener() {
super();
}
@Override
public void run() {
int x;
User32 user32 = User32.INSTANCE;
boolean res = user32.RegisterHotKey(Pointer.NULL, 1, User32.MOD_ALT | User32.MOD_CONTROL, WinKeys.VK_X);
if (!res) {
System.out.println("Couldn't register hotkey");
}
System.out.println("Starting and waiting");
while (!isInterrupted()) {
x = user32.MsgWaitForMultipleObjects(0, Pointer.NULL, true, 1000, User32.QS_HOTKEY);
if (x == 0) {
System.out.println("Key pressed");
}
}
}
}
( ) ALT+X. Key pressed , ( 0 ). , , "reset" , , 258 (258 == wait). , .
- , , , .