用Java JNA编写的密钥监听器。无法阻止线程

时间:2012-05-21 11:45:08

标签: java jna

我使用以下代码来监听全局关键事件:

Win32HookManager.java

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.HHOOK;
import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;
import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc;
import com.sun.jna.platform.win32.WinUser.MSG;


import java.awt.event.KeyEvent;

public class Win32HookManager {
    private static HHOOK keyboardHook;

    public static boolean installKeyboardHook(final NativeKeyboardListener listener) {
        final User32  lib  = User32.INSTANCE;
        final HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);

        final LowLevelKeyboardProc keyboardHookProc = new LowLevelKeyboardProc() {
            @Override
            public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
                NativeKeyboardEvent ev = null;
                long                ti = System.currentTimeMillis();
                boolean             nh = true;

                if (nCode >= 0) {
                    switch (wParam.intValue()) {
                        case WinUser.WM_KEYDOWN:
                        case WinUser.WM_SYSKEYDOWN:
                            ev = new NativeKeyboardEvent(KeyEvent.KEY_PRESSED, ti, 0, info.vkCode);
                            nh = listener.keyPressed(ev);
                            break;

                        case WinUser.WM_KEYUP:
                        case WinUser.WM_SYSKEYUP:
                            ev = new NativeKeyboardEvent(KeyEvent.KEY_RELEASED, ti, 0, info.vkCode);
                            nh = listener.keyReleased(ev);
                            break;
                    }
                }

                if(nh) {
                    return lib.CallNextHookEx(keyboardHook, nCode, wParam, info.getPointer());
                }
                return new LRESULT(1);
            }
        };

        new Thread() {
            @Override
            public void run() {
                keyboardHook = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHookProc, hMod, 0);
                msgLoop();
                lib.UnhookWindowsHookEx(keyboardHook);
            }
        }.start();

        return keyboardHook != null;
    }

    public static boolean uninstallKeyboardHook() {
        if(keyboardHook != null) {
            return User32.INSTANCE.UnhookWindowsHookEx(keyboardHook);
        }

        return false;
    }

    private static void msgLoop()
    {
        final User32 lib = User32.INSTANCE;

        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);
            }
        }
    }
}

NativeKeyboardListener

public interface NativeKeyboardListener {
    public boolean keyPressed (NativeKeyboardEvent e);
    public boolean keyReleased(NativeKeyboardEvent e);
}

NativeKeyboardEvent

public class NativeKeyboardEvent {
    private int  id;
    private int  keyCode;

    public NativeKeyboardEvent(int id, long when, int modifiers, int keyCode) {
        this.id        = id;
        this.keyCode   = keyCode;
    }

    public int getId() {
        return id;
    }

    public int getKeyCode() {
        return keyCode;
    }
}

不幸的是,它没有按照我的预期工作,即它检测到按下/释放按键的时间,但由于msgLoop方法中的GetMessage(),它无法完成由installKeyboardHook()方法启动的线程。是的,我可以停止听关键事件,但我无法阻止线程。但是,此代码中似乎需要GetMessage()。您是否看到此问题的解决方法?

谢谢!

3 个答案:

答案 0 :(得分:3)

正如@SLaks建议的那样,您需要某种标志来指示消息循环是否应该继续运行。然后,您可以使用PeekMessageJNA Doc),与GetMessage一样,从队列中检索消息,但不是阻止操作。然后您的消息循环应该更改为:

while (!shouldQuit) {
    while ((result = lib.PeekMessage(msg, null, 0, 0, 1)) != 0) {
        // ...
    }
}

答案 1 :(得分:1)

你应该制作一个private boolean shouldQuit标志,如果标志为真,则跳出消息循环。

然后,要停止该线程,只需将该标志设置为true。

答案 2 :(得分:0)

不要在消息处理线程中安装/卸载。

只有GetMessage处理(和退出检查)需要在附加Thread(应该是守护程序线程,BTW)中发生。安装和拆卸挂钩应该在其他地方进行。

查看JNA中的贡献keyboard hook,了解正确的过程示例。