Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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).

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?

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.

I am a bit confused about its usage. Where do I provide the window handler (hwnd)? Do I need to use the GetMesssage() function before using the GetMessagePos() ? Programer Beginner Apr 15, 2021 at 5:01 So if I have an application (let's say paint), I have its hwnd , I moved the virtual mouse in paint via WM_MOUSEMOVE , I won't be able to get the virtual cursor in paint with just hwnd ? Programer Beginner Apr 15, 2021 at 6:20 Yes, according to GetMessage : A handle to the window whose messages are to be retrieved. The window must belong to the current thread.So you can't get its internal messages only through hwnd. Zeus Apr 15, 2021 at 6:23 I updated my question, to be more specific. I unfortunately am trying to get the virtual cursor position of an external application, thus I have only its window handler. Programer Beginner Apr 15, 2021 at 11:47

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .