Java – Global (low level) Keyboard / Mouse Hook

I’d like to start my small blog with a neat little Java snippet. As I wanted to have a global key/mouse hook for my Java programs, I found a great little script using the JNI at Jotschi’s Blog. Unfortuately Jotschi doesn’t develop on any windows binaries anymore, so he can not provide enhancements. Therefore I took Jotschi’s code, fixed some bugs and enhanced especially the Java part.

Fixed some bugs, especially time consuming processes in the event handlers kept crashing the JNI (added a buffer mechansim)

Added support for all keyboard events (such as key-up and also the control keys [ALT, CTRL and SHIFT] are now easier to access)

Added support for all low level mouse events (such as mouse buttons)

It is now guranteed that the mouse coordinate is inside the screen bounds (before -1 or 1026 have been valid values at a 1024×800 pixel resolution)

The hook’s are now non-blocking, which means, as the last (non-deamon) thread ends, also the hook-threads end

The native libraries are now bundled into the jar files. So it is possible, but not required to specify them as a virtual machine parameter

Last but not least, I will try to enhance the Global Keyboard / Mouse Hook, if you have any ideas / problems with it. Feel free to write it into the comments below!

The general description: Global Keyboard / Mouse Hook for Java applications. Normally keyboard and mouse listeners in Java only work, if the registered component has the focus. If, for example, any window looses it’s focus (minimized) it isn’t possible to track any keyboard / mouse events anymore. Therefore we will have to use the JNI (Java Nativ Interface), to register a low level keyboard / mouse hook to the system. This is done using a native library (compiled on Windows 7 x64 using 32 & 64 bit JVM).

Have fun! Don’t forget toSince version 0.2 it is possible, but no longer necessary to download the “KeyboardHook.dll” and/or “MouseHook.dll” libraries and specify the libary path when starting your JVM using:

(If you specify the virtual machine parameter instead of using the bundled libraries it might gets you a little performance enhance)

Update (7.16.11): A new version (0.2) is out now. At first I lowercased the library names, which should sove some issues loading the libraries. The major change regards the loading mechanism of the libraries. With the new Native classes, it is no longer necesarry to specify the virtual machine parameter, because the libraries are bundled into the KeyboardHook and MouseHook archives. I think this is a great advantage, especially if you plan to bundle your application. The MouseHook.h/.cpp files are now present in the source bundle aswell. Thanks to Adam.

Update (7.21.11): I made a minor change in version (0.2.1). Due to a missing linker flag, the virtual machine was not able to load the native libraries on some systems. An java.lang.UnsatisfiedLinkError was thrown. This issue should be solved now.

Update (10.11.11): Version 0.3 released! Now compiled and tested on a 32 & 64 bit JVM. Libraries have been splitted into a 32 & 64 bit DLL. Before an java.lang.UnsatisfiedLinkError was thrown on 64 bit JVM’s. This issue should be solved now. Also the library has been compiled using Java 7.0 native libs. Packaged bundles now also include the 64 bit DLL.