System.Windows in Excel VBA

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Sure. ActiveX controls have a MouseMove event, as does the Userform and the userform controls. But what exactly is it you're trying to do?
 
Upvote 0
Thanks, yup I know the UserForm event but I want to detect any mouse movement at the system level and not inside a UserForm or ActiveX.
 
Upvote 0
I did see that it was a system level event, but I couldn't gauge from the question whether the key point of your question was re: the MouseMove event or that it was to be a system-wide event... I erred in favour of what I presumed was the most likely (my apologies). The short answer is, as far as I'm aware, there's no easy way to integrate .NET namespaces into VBA code. There may, however, be a workaround...

Like what you've referenced here, I'm also looking to leverage one of the .NET Namespaces in a project of mine (Windows.Media.OCR), and some super smart people in the AutoHotkey community have managed to do it. My default position is that if you can do it in Autohotkey, then you can do it in VBA because both languages would be using the same Win32 API method. That said, that approach isn't extremely straight forward.

Using just Win32 APIs, though, you could possibly detect mousemovement events anywhere on the screen (ie., not over ActiveX or userform/UF controls) with the GetCursorPos API. This returns the X/Y coordinates of the mouse anywhere on the screen. Using some kind of timer, you could use this APi to get the mouse cursor position on every tick and then test to see whether or not the coordinates matches the coordinates previous recorded. If they don't match then the mouse has moved.

Alternatively, you could possibly use another API to hook into the Mouse, but I would need to look into that a little bit more.

The point is - detecting any mouse movement is doable, but there may be an even simpler solution than the above depending on your specific need/use case.
 
Upvote 0
@Dan_W thanks, my need is just to detect mouse move anywhere in the screen. Then what is triggered does not really matter, right? I know the GetCursorPos but it's a bit too verbose and need to save variables etc so I was looking for 1-2 lines of code.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top