Java – click the mouse with JNA

I'm trying to use JNA to simulate mouse clicking on a window

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

He found the window and took it to the front But mouse clicks don't work Also send WM_ Close work What's wrong with mouse clicking? Test calculator and notepad The coordinates are relative to the window

Solution

Just guess: the click event should not be passed to the main window, but to the target button object itself At the given coordinates, when the actual click occurs, the button is located above the main window to "hide" it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>