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. Himanshu Halder says:

    It’s just awesome. Those librery are realy helpful.
    Thanks for those precious librery…. hat’s off..

  2. Suja MM says:

    Hi,

    Thanks this code it’s great. Very helpful.

    I have one questions, does the library works in Linux and Mac ??

    I mean if I develop a program in Java will it work in Windows, linux and Mac ??

    • Jan Java says:

      I don’t think so. It looks like this code is made specifically for windows and thus only runs on that. Native code is almost never cross-plattform (atleast I haven’t seen any up till now).

  3. ZDawg says:

    Thank you so much! :) I’ve been trying to figure this out for a while. I’m glad somebody found a solution.

  4. Sam says:

    Thanks for providing such a wonderful tool.

    I checked your native library code for key board listener. I see you pass the GlobalKeyListener object in and out of the DLL boundary. I am wondering if it is really necessary. Since the KeyEvent object is constructed in the Java code and the GlobalKeyListener finally arrives there, maybe we can just leave the GlobalKeyListener in the Java code.

  5. Rafael says:

    Thank you, great job!
    This tool will help me for sure,

    Thanks!

  6. Bartran says:

    Awesome!!!

    Thanks very much. Just tried your solution after figthing with JNI, JNA and JIntellitype.

    Your’s the best.

    Thanks!

  7. Markus says:

    Hi,
    thank you for the tool.
    But I have a problem:
    I use it with Windows 8 64bit and Eclipse.
    After starting the GlobelKeyListener for about 20 times, it doesn´t work any more. The rest of the program runs OK. But no keys are dedectet.
    In this situation:
    … close and restart Eclipse doesn´t help
    … close all other Programs doesn´t help
    … hibernate the Computer doesn´t help
    The only way to make it work again is to shut down the Computer und restart all again. Then it works ok for about the next 20 program starts.
    Can anybody help me ?

    Thank you, Markus

    • sach says:

      ya i am facing the same problem……do you have a solution for

      Or do you have a better way of getting input from keys with or without this?

  8. Benjamin says:

    Hi Markus,

    I have the same problem. GlobelKeyListener doesn’t work after about 20 times (No log) :-(.

    Platform : Windows 7 Professionnal 64 bits

    Benjamin.

  9. Corbin Stark says:

    Ditto ^ after about 20 times it renders every single keypress as up

  10. Shiinon says:

    Would love to use something like this (tried JNativeHook but after a few runs of my application it breaks and doesn’t work until I restart my computer). The only problem I’d have with this is here’s no wheel listener?

  11. Shiinon says:

    I take back what I said about JNativeHook, the problem was there but had already been patched when I posted my comment ^.

  12. […] I want to compile the KeyboardHook from Kristian Kraljic (http://kra.lc/blog/2011/07/java-global-system-hook/ ) with some added functionality in the library. First I tried to compile my KeyboardHook.cpp with […]