Search in sources :

Example 1 with LowLevelKeyboardProc

use of com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc in project jna by java-native-access.

the class KeyHook method main.

public static void main(String[] args) {
    final User32 lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook = new LowLevelKeyboardProc() {

        @Override
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
            if (nCode >= 0) {
                switch(wParam.intValue()) {
                    case WinUser.WM_KEYUP:
                    case WinUser.WM_KEYDOWN:
                    case WinUser.WM_SYSKEYUP:
                    case WinUser.WM_SYSKEYDOWN:
                        System.err.println("in callback, key=" + info.vkCode);
                        if (info.vkCode == 81) {
                            quit = true;
                        }
                }
            }
            Pointer ptr = info.getPointer();
            long peer = Pointer.nativeValue(ptr);
            return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));
        }
    };
    hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
    new Thread() {

        @Override
        public void run() {
            while (!quit) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
            System.err.println("unhook and exit");
            lib.UnhookWindowsHookEx(hhk);
            System.exit(0);
        }
    }.start();
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();
    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
        if (result == -1) {
            System.err.println("error in get message");
            break;
        } else {
            System.err.println("got message");
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
    }
    lib.UnhookWindowsHookEx(hhk);
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) KBDLLHOOKSTRUCT(com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) LowLevelKeyboardProc(com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) User32(com.sun.jna.platform.win32.User32)

Aggregations

Pointer (com.sun.jna.Pointer)1 User32 (com.sun.jna.platform.win32.User32)1 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)1 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)1 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)1 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)1 KBDLLHOOKSTRUCT (com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT)1 LowLevelKeyboardProc (com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc)1 MSG (com.sun.jna.platform.win32.WinUser.MSG)1