global KeyListener using JNA
I don't know about JNA solution, but there is a well-established global hotkey library called JIntelliType
EDIT: Correct answer to this problem was to use GetMessage instead of MsgWaitForMultipleObjects. I wrote a simple example using BridJ and it works great:
if (!RegisterHotKey(null, id, MOD_ALT | MOD_NOREPEAT, 0x42)) {
System.out.println("Error");
return;
}
Pointer<MSG> msgPointer = Pointer.allocate(MSG.class);
try {
while (GetMessage(msgPointer, null, 0, 0) != 0) {
MSG msg = msgPointer.get();
if (msg.message() == WM_HOTKEY && msg.wParam() == id) {
System.out.println("YEAH");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
UnregisterHotKey(null, id);
}
Listening to key presses in Java without a UI component
As far as I know, there's no way to do this in straight Java. Since keyboard commands are handled by the OS, the only way to get Java to do this is to write some low level JNI. I've done something like this by writing an X event handler in C++ (for *nix based OS). Since key events in Java are per GUI component, there's no way to do this on a global scale. When I wrote the X event handle, I had the challenge of dealing with the limitation that only a single application at a time can grab a key. (XGrabKey).
Related Topics
Run Java Class File from PHP Script on a Website
Problems Submitting a Login Form With Jsoup
How to Get Stock Quotes Using Google Finance API
How to Return Datasnapshot Value as a Result of a Method
Android Permission Doesn't Work Even If I Have Declared It
Sending Email in Android Using Javamail API Without Using the Default/Built-In App
How to Create Recyclerview With Multiple View Types
Why Does My Function That Calls an API or Launches a Coroutine Return an Empty or Null Value
Firebaselistadapter Not Pushing Individual Items For Chat App - Firebase-Ui 3.1
Android 6.0 Multiple Permissions
How to Pass a Bitmap Object from One Activity to Another
Inner Class Can Access But Not Update Values - Asynctask
Launching Google Maps Directions Via an Intent on Android
Android Activity Classnotfoundexception - Tried Everything
How to Get the Sharedpreferences from a Preferenceactivity in Android