Java – Global (low level) Keyboard / Mouse Hook
by Kristian Kraljic, February 9, 2016
About five years ago I started this blog by picking up an idea of Johannes Schüth (Jotschi) about a global keyboard and mouse listener for Java. Today I am very happy to announce the next major version of the library now available on GitHub!
Keyboard and mouse events in Java only work, if the registered component is in focus. For example, in case a window looses its focus (e.g. when minimized), it stops receiving any more events. Through a low-level system-wide hook it’s possible to deliver those events regardless. You’ll find all sources of the latest release, as well as binary bundles / prepackaged Java archives (JAR) on the GitHub project page.
The old post describes all changes I did to the initial version by Johannes (Jotschi). For the new major release I again reworked nearly every part of the library, to make it more stable and versatile. Here is what I did:
- Optimized the native library C code, fixed bugs and removed all parts requireing a C++ compiler in the first place.
- Renamed the event classes and listener interfaces and prepended
Global
to avoid conflicts with existing (Swing) event listeners. - Added support for
mouseWheel
events to theGlobalMouseListener
. - Again allowed negative "out of bounds" values for
GlobalMouseEvents
, to also track the mouse pointer off screen (e.g. on multi-monitor setups). - Improved the threading concept and implemented a native error handling. The
GlobalKeyboardHook
andGlobalMouseHook
constructors will now throw aUnsatisfiedLinkError
if the native libraries can not be loaded or aRuntimeException
in case hooking fails. - All the code has been moved to GitHub and binaries are now beeing continuously built by AppVeyor. Feel free to contribute on GitHub!
(Again) Last but not least, please share your ideas and problems in the comments. I will try to enhance the Global Keyboard / Mouse Hook based on your feedback. For the next release let’s see if we can get Linux and / or Mac OSX support going.
202 Comments
« Older CommentsNewer Comments »
« Older CommentsNewer Comments »
Hi Kristian,
I have the same problem. I compile the DLL’s on Visual Studio, but the size is tiny and when I try compile Java program launch this error:
Exception in thread “Thread-0” java.lang.UnsatisfiedLinkError: C:\…\Temp\keyboardhook-1808095537732618315.lib: %1 no es una aplicación Win32 válida
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at de.ksquared.system.keyboard.Native.load(Native.java:74)
at de.ksquared.system.keyboard.KeyboardHook.(KeyboardHook.java:77)
at de.ksquared.system.keyboard.PoolHook.run(KeyboardHook.java:45)
Can you helpme?
Thanks.
Just want to say thanks for your work. I’ve been searching this for awhile. It’s good to finally get a hold on your work. Thanks!!
Thank you very much for your work, i’ve been searching a lib like that since a long time and yours works perfectly !!!
Great job !
For some reason, the keyboard hook periodically stops working on my machine. I am using the stand-alone library. Whenever the keyboard hook stops working, I am forced to restart the computer in order for it to work again. I want to use this to distribute my free software but I am concerned about the reliability. Nonetheless, the work is still excellent and very much appreciated.
If there is any update on this issue, please make it available. Thanks so much for your hard work!
Big thanks, it’s works perfectly!
Thanks for your code, is it possible to lock events? for example lock mouse events? and do not let the user to use his mouse?
Thanks ;)
This is wonderful!! This has made my life very easy.
Will this work on any Operating system? (eg: Solaris,Linux, Mac etc)
Hello Krieya, no. At the moment this does only work on Windows machines, because it is directly accessing the Low-Level Keyboard API of Windows.
Sorry & regards, Kristian
Super good!
Just discovered this, exactly what I need for a pet project.
Any thoughts about doing the same, but with JNA instead of JNI? Presumably the hook functions could be called from User32 (or whatever it is) via JNA?
Hey Mikey, that’s basically a very good idea. I agree that JNA would be faster and easier to maintain. But I do not know if you could implement a low-level hook with it, as the hook is event-driven, and does not call the Windows API in an regular interval.
This would be definitiely worth a try, as the DLL’s are quite a pity to maintain and deliver.
Thanks for your suggestion & regards, Kristian
Hi, super cool project :)
could no get it to run, can someopne help me? the example-jar-file does nothing, and i dont know how to configure and use the code
excuser me sir do you have a code for trapping the alt+control+delete ?
Is it available for linux???
I plan to do so… yes.