JNA:关键侦听器+ JFrame [英] JNA: key listener + JFrame

查看:110
本文介绍了JNA:关键侦听器+ JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的应用程序,该应用程序在后台和JFrame中都有一个键盘侦听器.实际上,我使用以下代码创建侦听器:

I'm trying to write a simple application where I have a keyboard listener in the background and JFrame. Practically, I use the following code to create the listener:

public class KeyHook {
    private static volatile boolean quit;
    private static HHOOK hhk;
    private static LowLevelKeyboardProc keyboardHook;

    public static void main(String[] args) {
        final User32 lib = User32.INSTANCE;
        HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
        keyboardHook = new LowLevelKeyboardProc() {
            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;
                        }
                    }
                }
                return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
            }
        };
        hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
        System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
        new Thread() {
            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);
    }
}

我的要求是先创建监听器,然后再创建JFrame..问题是由于GetMessage,我不知道如何创建JFrame.现在,GetMessage阻止了当前线程,因此我无法在它之后创建JFrame.另一方面,我不能将其放在新的线程中,因为那样,侦听器将无法工作.希望您能理解这个问题:)有什么解决方法的想法吗?

My requirement is to create the listener as the first and after it JFrame. The problem is that I don't know how to create JFrame because of GetMessage. Now, GetMessage blocks the current thread so I can't create JFrame after it. On the other hand, I can't put it in a new Thread because then, the listener doesn't work. I hope you understand the issue :) Any ideas how to solve it?

提前谢谢!

推荐答案

好的,这是解决问题的方法:

Ok, this is what should be done to solve the problem:

我们扔掉这个片段:

new Thread() {
   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();

接下来,我们将来自main方法的代码包含在以下内容中:

and next we enclose code from main method in:

new Thread(new Runnable() {
...
}).start();

然后,我们可以自由输入例如:

Then, we are free to type e.g. :

startListening(); // key listener

JFrame frame = new JFrame();
...
SwingUtilities.invokeLater(new Runnable() {

   @Override
      public void run() {
         frame.setVisible(true);
      }
});

这篇关于JNA:关键侦听器+ JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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