Python win32gui get current position of the virtual cursor in a window

1.2k views Asked by At

I know that using win32gui.PostMessage(hwnd, win32con.WM_MOUSEMOVE, wParam, lParam) I can fake mouse movement to an unfocused application, without effecting my real mouse.

How can I get my current "fake mouse" position if win32api.GetCursorPos() returns the position of my real mouse cursor?

Edit #1:

I am using WM_MOUSEMOVE on an external application, thus the only thing I have is the application's window handle (HWND).

1

There are 1 answers

7
Zeus On

You can use GetMessagePos:

Retrieves the cursor position for the last message retrieved by the GetMessage function.

More reference: Possible to do MouseMove actions with PostMessage without hijacking the cursor?

Edit

If you only want to get its messages through the HWND of the external application, you need to use SetWindowsHookEx and make it into a dll to inject it into the external process.

You can use SetWindowsHookEx function, with WH_CALLWNDPROC or some other type of hook, and here is an example.