Claudiu Claudiu - 1 year ago 105
Python Question

win32: simulate a click without simulating mouse movement?

I'm trying to simulate a mouse click on a window. I currently have success doing this as follows (I'm using Python, but it should apply to general win32):


This works fine. However, if the click happens while I'm moving the mouse manually, the cursor position gets thrown off. Is there any way to send a click directly to a given
coordinate without moving the mouse there? I've tried something like the following with not much luck:

nx = x*65535/win32api.GetSystemMetrics(0)
ny = y*65535/win32api.GetSystemMetrics(1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN | \
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | \

Answer Source

Try WindowFromPoint() function:

    pt.x = 30; // This is your click coordinates
    pt.y = 30;

HWND hWnd = WindowFromPoint(pt);
LPARAM lParam = MAKELPARAM(pt.x, pt.y);
PostMessage(hWnd, WM_RBUTTONDOWN, MK_RBUTTON, lParam);
PostMessage(hWnd, WM_RBUTTONUP, MK_RBUTTON, lParam);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download