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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With