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.

115 Comments

  1. Pradeep says:

    Fantastic!!! Works soooooooo well!!!
    I have one question though. When we are typing on Windows pop-up windows (username and Password Windows) it doesn’t capture any. Is it because of the security that they are using or do we need to tweak something on this?

    • Hello Pradeep,

      > Is it because of the security that they are using…
      Actually that’s the case, yes. And I don’t think you can do anything about it either, as the low-level hook uses the Windows API, so they have all the mechanisms in hand to prevent your program from capturing the log-in username & password.

      Regards, Kristian

  2. vijay Gharal says:

    I am getting this issue can anyone help one the same ?

    Exception in thread “Thread-0” java.lang.UnsatisfiedLinkError: KeyboardHook.de.ksquared.system.keyboard.KeyboardHook.registerHook(LKeyboardHook/de/ksquared/system/keyboard/GlobalKeyListener;)V
    at KeyboardHook.de.ksquared.system.keyboard.KeyboardHook.registerHook(Native Method)
    at KeyboardHook.de.ksquared.system.keyboard.PoolHook.run(KeyboardHook.java:46)

  3. Raja says:

    Hi Guys,

    this is simply fantastic.. :)

    but can you provide code for mouse wheel listener as well.. I want to get notified whenever mouse wheel is moved..

    Thanks in advance.

    Raj

  4. Raja says:

    Kris,

    I should mention again, that’s really amazing work.

    Really thanks.

    But as posted earlier, I am in urgent need of Mouse Wheel Listener..

    Please help me. (hereisrajiv@gmail.com)

    Thanks,
    Raja M

    • Hello Raja,

      I am verry happy to announce that I just released version 2.0 of the library, featuring a mouseWheel event for the GlobalMouseListener. Please find the code on GitHub.

      Hope this helps. Regards,
      Kristian