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. Johny says:

    @punarbhava, simply make a static method and call that whenever the key event is fired with the pressed key value in it’s argument.

    In the method you can have all your logic whatever you want to do with it. For your understanding, i would recommend you to read little about threads in java :)

  2. Dipti says:

    Fair eognuh, and I do agree with your points. As for it being an optimization, yes. But it is one that matters, no? Often the reason this comes up, I thought, was when people are lamenting the lack of “value types” in java. Well, that and interview questions.

  3. wlfbck says:

    Hey,
    first of all thanks for this! But i have a little problem, your examples aren’t working.

    In KeyboadHookTest:
    both overrides don’t override anything (maybe because KeyAdapter isnt abstract?).
    event.getVirtualKeyCode – this method does not exist, but you probably changed it to getKeyCode?

    downloaded the binary btw.

  4. Kristian Kraljic says:

    Hello wlfbck,

    KeyAdapter isn’t an abstract class, thats correct, KeyListener is. But the method is overwritten in any case. Also the method getVirtualKeyCode exists.

    Please make sure that you are using the right imports. From your explanation it sounds like you are using java.awt.event.KeyAdapter. The right import in this example would be de.ksquared.system.keyboard.KeyAdapter.

    Hope this helps.

    Regards,
    Kristian

  5. nitin says:

    Dear Kristian,

    MouseHookTest program is working fine for both keys and also shows values for mouse movement.But it does not show value while we rotate mouse wheel(up & down).

  6. amit says:

    what to do to run the same on “Linux” system?

  7. Kristian Kraljic says:

    @nitin: I will have a look at it in the next version of the library.

    @admit: Please have a look at my comment from the 11.17.11

    Regards,
    Kristian

  8. […] from there I was able to get to this article with a low level hook implementation: http://ksquared.de/blog/2011/07/java-global-system-hook/ Tagged: GUIinputJavajava-util-scannermagnetic-cardsquestions /* * * CONFIGURATION […]

  9. Fanadin says:

    Dear Kristian

    Mousehook runs fine on a 32bit Machine, but on my Windows 7 64 bit nothing happens. How can i Compile this for Windows 7 64 bit

  10. dazz007 says:

    Hello.
    I am totally beginner in JNI, and I have big problem to compile this. Could someone write step by step what should I do?

  11. choi says:

    Dear Kristian
    Thank you so much! It’s awesome :D