使用 JNA 单击鼠标

发布于 2025-01-06 04:28:13 字数 969 浏览 5 评论 0原文

我正在尝试使用 JNA 模拟鼠标在窗口中的单击。

public class App {

public static final int WM_LBUTTONUP = 514;
public static final int WM_LBUTTONDOWN = 513;
public static final int WM_LBUTTONDBLCLK = 0x203;
static int WM_CLOSE = 0x10;
final static String winTitle = "Untitled - Notepad";

public static void main(String[] args) throws InterruptedException {
    User32Extra user32 = (User32Extra) Native.loadLibrary("user32", User32Extra.class, W32APIOptions.DEFAULT_OPTIONS);

    WinDef.HWND hwnd = user32.FindWindow(null, winTitle);
    user32.SetForegroundWindow(hwnd);
    Thread.sleep(1000);

    long y = 77 + (22 << 16);//x + (y << 16)
    WinDef.LPARAM l = new WinDef.LPARAM(y);
    WinDef.WPARAM w = new WinDef.WPARAM(0);
    user32.PostMessage(hwnd, WM_LBUTTONDOWN, w, l);
    Thread.sleep(1000);
    user32.PostMessage(hwnd, WM_LBUTTONUP, w, l);
}
}

它找到窗口并将其带到前面。但鼠标点击不起作用。发送 WM_CLOSE 也有效。 鼠标点击有什么问题? 在计算器和记事本上测试。 坐标是相对于窗口的。

I'm trying to simulate mouse click at window with JNA.

public class App {

public static final int WM_LBUTTONUP = 514;
public static final int WM_LBUTTONDOWN = 513;
public static final int WM_LBUTTONDBLCLK = 0x203;
static int WM_CLOSE = 0x10;
final static String winTitle = "Untitled - Notepad";

public static void main(String[] args) throws InterruptedException {
    User32Extra user32 = (User32Extra) Native.loadLibrary("user32", User32Extra.class, W32APIOptions.DEFAULT_OPTIONS);

    WinDef.HWND hwnd = user32.FindWindow(null, winTitle);
    user32.SetForegroundWindow(hwnd);
    Thread.sleep(1000);

    long y = 77 + (22 << 16);//x + (y << 16)
    WinDef.LPARAM l = new WinDef.LPARAM(y);
    WinDef.WPARAM w = new WinDef.WPARAM(0);
    user32.PostMessage(hwnd, WM_LBUTTONDOWN, w, l);
    Thread.sleep(1000);
    user32.PostMessage(hwnd, WM_LBUTTONUP, w, l);
}
}

It find the window and bring it to front. but mouse click doesn't work. Also sending WM_CLOSE works.
What's wrong with mouse click?
Tested on calculator and notepad.
Coordinates are relative to the window.

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

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

发布评论

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

评论(1

九命猫 2025-01-13 04:28:13

只是一个大胆的猜测:单击事件不应传递到主窗口,而应传递到目标按钮对象本身。在给定的坐标上,按钮位于主窗口上方,当真正的单击发生时,按钮会“隐藏”它。

Just a wild guess: The click events should not be delivered to the main window but to the destination button objects themselves. On the given coordinates the button lay above the main window "hiding" it when a real click happens.

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