Kristian Kraljić You may compare programming with poetry,
a harmonic script is a piece of creativity which will last forever.
- Kristian Kraljić

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!

system-hook-logo

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 the GlobalMouseListener.
  • 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 and GlobalMouseHook constructors will now throw a UnsatisfiedLinkError if the native libraries can not be loaded or a RuntimeException 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

  1. xouanar says:

    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.

  2. Jerry says:

    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!!

  3. Pumbaa says:

    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 !

  4. Chris says:

    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!

  5. Ilya says:

    Big thanks, it’s works perfectly!

  6. Mohammad says:

    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 ;)

  7. krieya says:

    This is wonderful!! This has made my life very easy.

    Will this work on any Operating system? (eg: Solaris,Linux, Mac etc)

    • Kristian Kraljic says:

      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

  8. oakley brillen outlet says:

    Super good!

  9. Mikey says:

    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?

    • Kristian Kraljic says:

      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

  10. markus schnepf says:

    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

  11. Alec jones Nuyad says:

    excuser me sir do you have a code for trapping the alt+control+delete ?

  12. Pavan Bahuguni says:

    Is it available for linux???