0

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.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • I don't like it very much but...what about a simple flag? bool _mouseMovedByApplication. When MouseMove event handler will be called and _mouseMovedByApplication is false then it has to stop timer. – Adriano Repetti Jan 14 '14 at 09:32
  • @Adriano That needs to be more complex than a single bool, as WM_MOUSEMOVE event are asynchronous (Posted). – manuell Jan 14 '14 at 09:46
  • @manuell true, Queue may works better but I wouldn't start with a (slightly) more complicated solution if simpler one works. Well to be sure he should detach event handler, change location, empty message queue, attach handler again. It may be too much. – Adriano Repetti Jan 14 '14 at 09:49

2 Answers2

3

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.

Paul
  • 6,061
  • 6
  • 39
  • 70
  • Wouldn't this also detect other applications moving the mouse programatically as being injected, tho'? This may be desired, but I would expect the OP wants to differentiate between moves they generate, and any other moves. – icabod Jan 14 '14 at 10:24
  • Yes, this will detect other applications, too, unless they have the `UIAccess` attribute as documented [here](http://msdn.microsoft.com/en-us/library/windows/desktop/hh448797.aspx). – Paul Jan 14 '14 at 10:29
2

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.

manuell
  • 7,528
  • 5
  • 31
  • 58