使用 JNA 单击鼠标
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是一个大胆的猜测:单击事件不应传递到主窗口,而应传递到目标按钮对象本身。在给定的坐标上,按钮位于主窗口上方,当真正的单击发生时,按钮会“隐藏”它。
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.