使用 jna 进行 keyhook 和消费

发布于 2024-12-21 18:31:57 字数 4866 浏览 2 评论 0原文

我正在制作一个自动点击器,它使用 jna 来挂钩来自键盘和鼠标的全局输入。对于键盘钩子,我使用 http://code.google.com/p/goldriver/source/browse/trunk/king/src/jnacontrib/w32keyhook/KeyHook.java?r=36

我想知道是否有任何可能的方法来消耗按键事件,以便其他应用程序不处理它?

修复了返回新的 LRESULT (1);

现在我遇到了一个问题,无法继续执行其余代码,这是源代码。我的程序一直监听键盘输入,甚至不继续显示 GUI。

public class GUI extends javax.swing.JFrame{

ArrayList<MEvent> events;

public static final int RUNNING = 0;
public static final int PAUSED = 1;
public static final int STOPPED = 2;
public static final int LISTENING = 3;

private int process = STOPPED;
private String display;

private JTable Events;
DefaultTableModel list;
Loader loader;
private static MouseHook mh;
static private KeyHook kh;
static GUI gui;
Robot robot;

/** Creates new form GUI */
public GUI() {
    initComponents();
    loader = new Loader(this);
    events = new ArrayList<MEvent>();
    list = new DefaultTableModel();
    mh = new MouseHook(this,list);
    mh.setMouseHook();

    list.addColumn("Type");
    list.addColumn("X");
    list.addColumn("Y");
    list.addColumn("Sleep");
    try {
        robot = new Robot();
    } catch (AWTException ex) {}


    displayProcess(process);
    Events.setModel(list);
    kh = new KeyHook(this);
    kh.run();
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            gui = new GUI();
            gui.setVisible(true);
        }
    });


}

}

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

private GUI gui;
User32 lib;
HMODULE hMod;
public boolean isHooked = false;

public KeyHook(final GUI gui) {
    this.gui = gui;
    lib = User32.INSTANCE;
    hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    Native.setProtected(true);
}

@Override
public void run() {
    keyboardHook = new LowLevelKeyboardProc() {
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
            if (nCode >= 0) {
                switch (wParam.intValue()) {
                    case WinUser.WM_KEYUP:
                        switch(info.vkCode){
                            //F7
                            case 0x76:
                                System.out.println("F7");
                                gui.listen();
                                break;

                            //F8
                            case 0x77:
                                System.out.println("F8");
                                gui.stopListening();
                                break;
                            //F9
                            case 0x78:
                                //System.out.println("F9");
                                //gui.start();
                                break;
                            //F10
                            case 0x79:
                                //gui.pause();
                                break;
                            //F11
                            case 0x7A:
                                //gui.stop();
                                break;
                            //ESC
                            case 0x1B:
                                quit = true;
                                break;
                        }
                        break;
                    case WinUser.WM_KEYDOWN:

                       break;
                    case WinUser.WM_SYSKEYUP:

                        break;
                    case WinUser.WM_SYSKEYDOWN:

                        break;
                }
            }
            return new LRESULT(1);//lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());//
        }
    };
    hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    //noinspection ConstantConditions
    new Thread() {
        public void run() {
            while (!quit) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            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);
}
}

I'm making an auto clicker that uses jna to hook global input from the keyboard and mouse. For the keyboard hook im using http://code.google.com/p/goldriver/source/browse/trunk/king/src/jnacontrib/w32keyhook/KeyHook.java?r=36.

I was wondering if there was any possible way to consume the key event so other applications don't process it?

Fixed with return new LRESULT (1);

Now I'm having a problem with it not continuing with the rest of the code, here is the source. My program keeps listening for keyboard input and doesn't continue to even show the GUI.

public class GUI extends javax.swing.JFrame{

ArrayList<MEvent> events;

public static final int RUNNING = 0;
public static final int PAUSED = 1;
public static final int STOPPED = 2;
public static final int LISTENING = 3;

private int process = STOPPED;
private String display;

private JTable Events;
DefaultTableModel list;
Loader loader;
private static MouseHook mh;
static private KeyHook kh;
static GUI gui;
Robot robot;

/** Creates new form GUI */
public GUI() {
    initComponents();
    loader = new Loader(this);
    events = new ArrayList<MEvent>();
    list = new DefaultTableModel();
    mh = new MouseHook(this,list);
    mh.setMouseHook();

    list.addColumn("Type");
    list.addColumn("X");
    list.addColumn("Y");
    list.addColumn("Sleep");
    try {
        robot = new Robot();
    } catch (AWTException ex) {}


    displayProcess(process);
    Events.setModel(list);
    kh = new KeyHook(this);
    kh.run();
}

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            gui = new GUI();
            gui.setVisible(true);
        }
    });


}

}

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

private GUI gui;
User32 lib;
HMODULE hMod;
public boolean isHooked = false;

public KeyHook(final GUI gui) {
    this.gui = gui;
    lib = User32.INSTANCE;
    hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    Native.setProtected(true);
}

@Override
public void run() {
    keyboardHook = new LowLevelKeyboardProc() {
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
            if (nCode >= 0) {
                switch (wParam.intValue()) {
                    case WinUser.WM_KEYUP:
                        switch(info.vkCode){
                            //F7
                            case 0x76:
                                System.out.println("F7");
                                gui.listen();
                                break;

                            //F8
                            case 0x77:
                                System.out.println("F8");
                                gui.stopListening();
                                break;
                            //F9
                            case 0x78:
                                //System.out.println("F9");
                                //gui.start();
                                break;
                            //F10
                            case 0x79:
                                //gui.pause();
                                break;
                            //F11
                            case 0x7A:
                                //gui.stop();
                                break;
                            //ESC
                            case 0x1B:
                                quit = true;
                                break;
                        }
                        break;
                    case WinUser.WM_KEYDOWN:

                       break;
                    case WinUser.WM_SYSKEYUP:

                        break;
                    case WinUser.WM_SYSKEYDOWN:

                        break;
                }
            }
            return new LRESULT(1);//lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());//
        }
    };
    hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    //noinspection ConstantConditions
    new Thread() {
        public void run() {
            while (!quit) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            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);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-12-28 18:31:57

是的,不要

return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());

在回调方法中调用,...但这样做有点邪恶,不是吗?即使不是邪恶,也有潜在的危险。

Yeah, don't call

return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());

In the call back method, ... but that's kind of an evil thing to do, isn't it? And if not evil, potentially dangerous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文