JNA Keyboard Hook in Windows

It appears you need to call GetMessage or PeekMessage, which is odd – it is not mentioned in the documentation for Hooks or LowLevelKeyboardProc. I don’t know enough about this part of the API to guess at the reason.

I just used the example classes:

import com.sun.jna.examples.win32.*;

public class Callback {
  public static User32.HHOOK hHook;
  public static User32.LowLevelKeyboardProc lpfn;
  public static volatile boolean quit = false;

  public static void main(String[] args) throws Exception {
    W32API.HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    lpfn = new User32.LowLevelKeyboardProc() {
      public W32API.LRESULT callback(int nCode, W32API.WPARAM wParam,
          User32.KBDLLHOOKSTRUCT lParam) {
        System.out.println("here");
        quit = true;
        return User32.INSTANCE.CallNextHookEx(hHook, nCode, wParam, lParam
            .getPointer());
      }
    };
    hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lpfn, hMod,
        0);
    if (hHook == null)
      return;
    User32.MSG msg = new User32.MSG();
    while (!quit) {
      User32.INSTANCE.PeekMessage(msg, null, 0, 0, 0);
      Thread.sleep(100);
    }
    if (User32.INSTANCE.UnhookWindowsHookEx(hHook))
      System.out.println("Unhooked");
  }
}

Leave a Comment