用JNA鼠标单击 [英] Mouse click with JNA

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

问题描述

我正在尝试使用JNA模拟鼠标在窗口上的点击.

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);
}
}

它找到窗口并将其放在前面.但鼠标单击不起作用.同样发送WM_CLOSE的作品. 鼠标单击有什么问题? 经过计算器和记事本测试. 坐标是相对于窗口的.

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.

推荐答案

只是一个疯狂的猜测:click事件不应传递到主窗口,而应传递到目标按钮对象本身.在给定的坐标上,当真正点击时,按钮位于主窗口上方,隐藏"该窗口.

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.

这篇关于用JNA鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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