"全局" KeyListener使用JNA [英] "global" KeyListener using JNA

查看:114
本文介绍了"全局" KeyListener使用JNA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows下运行的程序计划制作可以在不同的密钥runnig上映射不同的宏。问题是 - 当应用程序没有被聚焦时,如何使Java听到按下的键。

I have in plan making of program in Java running under Windows that can map diffenrent "macros" on different keys runnig at background. Problem is - how to make Java listen to keys pressed when the application is not being focused.

我发现很多意见认为这是不可能的。但我也发现了Stefano在这里写的这个 。对我来说,这个解决方案不够好,至少没有一个重要的信息。函数 MsgWaitForMultipleObjects()如果未按下该键,则返回一个值...这没关系。按键后,它返回不同的值......如果按键事件发生后函数不会返回相同的值,那就没问题。

I have found lot of opinions that this is not possible. But I have also found this written by Stefano here on SO. This solution is, well, not good enough for me, at least there is not one impotant information. The function MsgWaitForMultipleObjects() returns one value if the key is not pressed...that's ok. After key press, it returns different value...that would be ok, if the function wouldn't return the same value whatever happens after the key press event.

以下是测试此的线程:

public class KeyListener extends Thread {

    /**
     * Constructor
     */
    public KeyListener() {
        super();
    }

    /**
     * RUN method
     */
    @Override
    public void run() {
        int x;
        User32 user32 = User32.INSTANCE;

        boolean res = user32.RegisterHotKey(Pointer.NULL, 1, User32.MOD_ALT | User32.MOD_CONTROL, WinKeys.VK_X);
        if (!res) {
            System.out.println("Couldn't register hotkey");
        }
        System.out.println("Starting and waiting");

        while (!isInterrupted()) {
            x = user32.MsgWaitForMultipleObjects(0, Pointer.NULL, true, 1000, User32.QS_HOTKEY);

            if (x == 0) {
                System.out.println("Key pressed");
            }
        }
    }
}

这个小程序(使用此线程)在按 ALT + X 时作出反应。按下此按钮后,文本按键将写入控制台,直到程序停止(该函数始终返回0)。在我看来,可能的解决方案有一些重置功能,所以它会再次等待按键并再次返回 258 258 ==等待)。但我不知道该怎么做。

This little program (using this thread) reacts on pressing ALT+X. After this is pressed, the text Key pressed is written out to the console until the program stops (the function returns 0 all the time). Possible solution is in my opinion some "reset" of the function so it will wait for the key press again and return 258 again (258 == waiting). But I have no idea how to do this.

如果有人知道,怎么做,或者还有其他解决方案,我将不胜感激。

If somebody knows, how to do this, or is there is another solution, I would be grateful for any information.

推荐答案

我不了解JNA解决方案,但有一个成熟的全球热键库名为 JIntelliType

I don't know about JNA solution, but there is a well-established global hotkey library called JIntelliType

编辑:对此问题的正确答案是使用GetMessage而不是MsgWaitForMultipleObjects。我用BridJ编写了一个简单的例子,效果很好:

Correct answer to this problem was to use GetMessage instead of MsgWaitForMultipleObjects. I wrote a simple example using BridJ and it works great:

       if (!RegisterHotKey(null, id, MOD_ALT | MOD_NOREPEAT, 0x42)) {
            System.out.println("Error");
            return;
        }

        Pointer<MSG> msgPointer = Pointer.allocate(MSG.class);

        try {
            while (GetMessage(msgPointer, null, 0, 0) != 0) {
                MSG msg = msgPointer.get();
                if (msg.message() == WM_HOTKEY && msg.wParam() == id) {
                    System.out.println("YEAH");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            UnregisterHotKey(null, id);
        }

这篇关于&QUOT;全局&QUOT; KeyListener使用JNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆