Search code examples
javakeystroke

Detect keystroke without Textboxes in Java


I am advised to use JNI to detect keystroke from Keyboard , I have no idea about it. Been doing it using textboxes all this while. Any alternate way to achieve this?. The application should detect a keystroke which is pressed by a user. It should detect even when the focus is not on it while the application is still running in the background.


Solution

  • You can use JNA to create low-level key listener:

    https://github.com/twall/jna/

    Sadly there is no cross-platform solution for this, here is KeyHook example for Windows:

    https://github.com/twall/jna/blob/master/contrib/w32keyhook/com/sun/jna/contrib/demo/KeyHook.java