So I'm making an application that basically clones the mouse. You create a clone, set it's desired x/y offset from the original mouse and it'll be drawn to the screen with the Windows API. Now I know there are multiple posts asking this question however they've either not worked for me or require the handle of a window which is limiting since my application isn't meant for just one window. The closest I've gotten to work was mouse_event
from user32.dll
however it ignores the x/y position I give it and just clicks where the original cursor is. How do I simulate a mouse click on any window, even the desktop, without moving the original mouse cursor to the given position?
Asked
Active
Viewed 560 times
1

Crimson Bloom
- 287
- 2
- 13
-
As I understand you want to invoke mouse events (click, move etc) on destination area( any other window or desktop) based on original mouse pointer location. e.g. if original mouse pointer is at (10, 20) location on screen and offset you set is (5, 5). Now mouse is click on original location then you want same click event should occur as (15,25) location as well. – Nilesh Shinde Jul 20 '20 at 15:16
-
Kinda, I don't want mouse move events, just clicks at a specified location without moving the cursor. – Crimson Bloom Jul 20 '20 at 15:43
-
Everything I read says the cursor has to move. So you have to save the current position, move the cursor, do the click, and then move the cursor back. – Loathing Jul 21 '20 at 02:00
-
1Could you please describe your requirement in detail, may be use-case wise.? – Nilesh Shinde Jul 21 '20 at 09:10
-
@Loathing I can't move the cursor as the original mouse is meant to be usable while the clones are simulating clicks. – Crimson Bloom Jul 21 '20 at 12:41
-
@CrimsonBloom The `AutomationElement` might be an alternative solution. It has an `Invoke` method that can be used to simulate a click on a control, like a button, but it's not actually a click event. You can see this [example](https://stackoverflow.com/a/13325905/904156). – Loathing Jul 21 '20 at 22:23
-
@Loathing How would this work on another window, say MS Paint for example? – Crimson Bloom Jul 22 '20 at 15:26
-
As @NileshShinde said, you need to provide more detail in your question. – Loathing Jul 22 '20 at 23:11
-
@Loathing Basically I want multiple cursors that do exactly what the original cursor does but at an offset. Say I have two clones with different offsets, I click in MS Paint with the original cursor and draw a dot, after a changeable delay like 50ms clone1 will click and draw a dot then another 50ms before clone2 clicks and draws. After one click with the mouse I will have three dots in different locations on the MS Paint canvas. Still this isn't to be limited to only paint however, it should be able to work on any window or even multiple say like a game like how multiboxing works in WoW. – Crimson Bloom Jul 23 '20 at 06:42
-
The original cursor position will be saved after it's initial click for the clones to work out where they're to click, during this time the original cursor will still be able to move around without effecting where the clones will click. – Crimson Bloom Jul 23 '20 at 06:45
-
2You should check on Windows Messaging Queue. Specifically check PostMessage API which will send custom messages to other application windows. Other application windows should register to this custom message. Passed message will communicate info about X,Y coordinates and click status. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea?redirectedfrom=MSDN – Nilesh Shinde Jul 23 '20 at 12:40
-
When mouse click event occurs, source application window will call PostMessage API to send X,Y cords and mouse status. Recipient application windows will add this custom message type in the application Message map and provide handler function. So on original mouse click these handler function will get called in different application Windows. I am using same logic in my C++ application to propagate application events, but its within single process. You can extend it to across multiple processes. – Nilesh Shinde Jul 23 '20 at 12:42
-
@NileshShinde I've been testing PostMessageA out and all it does is click on the start button, ignoring x/y position. It also requires a window's handle which is difficult to use with multiple windows, especially if they're of the same application. – Crimson Bloom Jul 23 '20 at 20:26
-
If the clone1's x/y position is over an unfocused window, that window should receive it's click and clone2's x/y is over a different window that'll be clicked also. With the original and two clones, I should be able to click n three different windows with a single press on the mouse. – Crimson Bloom Jul 23 '20 at 20:30
-
1@NileshShinde Thank you for your help, I managed to get it working with `PostMessageA` by combining it with `WindowFromPoint` & `GetWindowRect`. – Crimson Bloom Jul 23 '20 at 22:00
-
@CrimsonBloom "without moving the cursor" is impossible according to https://stackoverflow.com/a/34846256/3809427 unless the UI you want to click has HWND. Did you find a solution? – Loran Mar 20 '23 at 08:27