VBA code to detect mouse movement run a certain code.

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,573
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?
 

Forum statistics

Threads
1,077,855
Messages
5,336,782
Members
399,102
Latest member
chudson1

Some videos you may like

This Week's Hot Topics

Top