Distinguish between user generated and my own application generated mouse moves

341 Views Asked by At

My WinForms app moves the mouse cursor about the form simply by setting the Cursor.Position property in some kind of a loop with a timer.

I would like this movement to continue only till the user does not wield the mouse himself to move it. As soon as he or she does, I want my code to stop.

Therefore, I need a way to distinguish between mouse moves that the user generated mouse movement sent vs. those that my application generates.

2

There are 2 best solutions below

0
On

If you use the Win32 SendInput API to position the mouse cursor, you can set a custom dwExtraInfo value in the MOUSEINPUT structure.

When processing an asynchronous (posted) message, you can use the Win32 API GetMessageExtraInfo to retrieve the custom value.

2
On

A straightforward solution for Windows 8 and up: use the GetCurrentInputMessageSource function.

if originId is IMO_INJECTED, the input is emulated. Otherwise, it usually equals IMO_HARDWARE.