VBA code to detect mouse movement run a certain code.

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have a sub that I won't to run anything there is a mouse movement or a key is pressed.





It's the stopTimer Sub I found on the web somewhere. So I have this voice code a run periodically. So when I move the move or press a key, then I will like to call the "stiptimer" sub.





I will be glad if I can find a way to interrupt and stop the voice immediately the mouse or key movement is detected.





This is a code from @Tom Urtis at this thread :https://www.mrexcel.com/forum/excel-questions/401643-detect-mouse-movement.html





Code:
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


Public Type POINTAPI


x As Long


y As Long


End Type


 


Sub PositionXY()


Dim lngCurPos As POINTAPI


Do


GetCursorPos lngCurPos


Range("A1").Value = "X: " & lngCurPos.x & " Y: " & lngCurPos.y


DoEvents


Loop


End Sub





Now here is the voice I am playing:


Code:
Public RunWhen As Double


Public Const cRunWhat = "My_Procedure"








Sub StartTimer()


        RunWhen = Now + TimeSerial(0, 0, 10)


        Application. OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True


End Sub





Sub Stopimer()


        Application. OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False


End Sub





Sub My_Procedure ()





        With Application


                 .Speech. Speak "The current time is " & Time, SpeakAsync:=True





        If .Wait (Now + TimeValue("00:00:05")) Then


            .Speech. Speak "", SpeakAsync:=True, Purge:=True


         End If


         End With





     Call StartTimer


End Sub

So from the above codes I am interrupting the voice after five seconds.

My wish is to be able to interrupt and call the "StopTimer" sub whenever there is a mouse or key movement detected

Any help?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,814
Messages
6,181,123
Members
453,021
Latest member
Justyna P

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