如何使用JNA配合java来检测点击

问题描述 投票:0回答:1

如果我尝试编写下面的代码,我会收到以下错误

线程“main”java.lang.UnsatisfiedLinkError中出现异常:查找函数“SetWindowsHookEx”时出错:找不到指定的过程。

at com.sun.jna.Function.<init>(Function.java:252)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:620)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:596)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:582)
at com.sun.jna.Library$Handler.invoke(Library.java:248)
at jdk.proxy1/jdk.proxy1.$Proxy0.SetWindowsHookEx(Unknown Source)
at tester.test.main(test.java:38)

我正在使用机器翻译,因此此英文文本可能是错误的。 另外,由于这是我第一次问这个问题,如果您能告诉我我可以改进的地方,那就太好了。

我编写这段代码时认为单击时该消息会出现在控制台中。 但我遇到了一个我不明白的错误。

package tester;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

public class test {
    public interface User32Ext extends User32 {
        User32Ext INSTANCE = Native.load("user32", User32Ext.class);

        int WM_LBUTTONDOWN = 0x0201;
        int WM_RBUTTONDOWN = 0x0204;

        WinUser.HHOOK SetWindowsHookEx(int idHook, HOOKPROC lpfn, WinDef.HINSTANCE hMod, WinDef.DWORD dwThreadId);

        WinDef.LRESULT CallNextHookEx(WinUser.HHOOK hhk, int nCode, WinDef.WPARAM wParam, WinUser.MSLLHOOKSTRUCT lParam);

        boolean UnhookWindowsHookEx(WinUser.HHOOK hhk);
    }

    public static interface HOOKPROC extends WinUser.LowLevelMouseProc {
    }

    public static void main(String args[]) {
        User32Ext user32 = User32Ext.INSTANCE;
        HOOKPROC hookProc = (int nCode, WinDef.WPARAM wParam, WinUser.MSLLHOOKSTRUCT lParam) -> {
            if (nCode >= 0) {
                if (wParam.intValue() == User32Ext.WM_LBUTTONDOWN) {
                    System.out.println("L");
                } else if (wParam.intValue() == User32Ext.WM_RBUTTONDOWN) {
                    System.out.println("R");
                }
            }
            return user32.CallNextHookEx(null, nCode, wParam, lParam);
        };

        WinUser.HHOOK hhk = user32.SetWindowsHookEx(WinUser.WH_MOUSE_LL, hookProc, null, 0);

        if (hhk == null) {
            System.err.println("Failed to set up a hook.");
            System.exit(1);
        }
        WinUser.MSG msg = new WinUser.MSG();
        while (User32.INSTANCE.GetMessage(msg, null, 0, 0) != 0) {
            User32.INSTANCE.TranslateMessage(msg);
            User32.INSTANCE.DispatchMessage(msg);
        }

        user32.UnhookWindowsHookEx(hhk);
    }
}

java jna
1个回答
0
投票

这与注册和创建任何其他处理程序没有太大区别。

首先,您需要在实现 NativeMouseListener 的适当类中创建处理程序(库位于 org.jnativehook.* 中):

public void nativeMouseClicked(NativeMouseEvent e) {
    if (e.getButton() == NativeMouseEvent.BUTTON3) {
        // do something
    }
}

(此时您也可以像这样添加nativeMousePressed和nativeMouseReleased)

其次你需要注册处理程序:

    try {
        GlobalScreen.registerNativeHook();
        GlobalScreen.addNativeMouseListener(new YourClass());
    } catch (Exception e) {
        e.printStackTrace();
    }

我只展示了帮助您从这一点继续前进的部分代码。如果您有疑问 - 请询问。

© www.soinside.com 2019 - 2024. All rights reserved.