Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse click with JNA

Tags:

java

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

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.

like image 712
NullPointer Avatar asked Feb 15 '12 12:02

NullPointer


1 Answers

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.

like image 98
A.H. Avatar answered Oct 21 '22 05:10

A.H.