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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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