Mouse Movement Detect via Custom OnEvent>

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Mouse Movement Detect via Custom OnEvent>

Post by JRL » Thu Oct 19, 2023 5:31 pm

Script to detect mouse movement via a custom OnEvent> allows the script to know when the mouse cursor is moving and when the mouse cursor is stopped.

The three OnEvent> parameters needed for a custom event handler are the "event parameter" which I have named "srtTestForMouseMovement". The "extra parameter" which I have named "vDeltaMouse". Lastly the "trigger parameter" which I have called "srtRunThisWhenMouseMoves"

While the rest of the script is processing, Macro Scheduler runs the "event parameter" subroutine up to 64 times per second waiting for the variable name defined in "extra parameter" to have a text value "TRUE". When "extra parameter" variable value becomes equal to the text "TRUE" the "trigger parameter" subroutine is run.

In this case "srtTestForMouseMovement" checks the location of the mouse cursor approximately every 0.015 seconds and compares the X and Y coordinates to the previous X and Y coordinates, if either of the coordinates change, the mouse must be moving and the variable "vDeltaMouse" is set to the text value "TRUE". When "vDeltaMouse" becomes "TRUE", the "srtRunThisWhenMouseMoves" subroutine is executed.

Don't forget that the trigger parameter subroutine is an ideal place to set the extra parameter variable to something other than the text "TRUE". The variable value needs to be reset otherwise the trigger parameter subroutine will never stop executing. In the script below notice the first line of the "srtRunThisWhenMouseMoves" subroutine sets the variable "vDeltaMouse" to the text value "FALSE".

Code: Select all

GetCursorPos>vCurXold,vCurYold
Let>vDeltaMouse=FALSE
Message>

OnEvent>Custom,srtTestForMouseMovement,vDeltaMouse,srtRunThisWhenMouseMoves

SRT>srtTestForMouseMovement
  GetCursorPos>vCurXnew,vCurYnew
  If>{(%vCurXold%-%vCurXnew%<>0)or(%vCurYold%-%vCurYnew%<>0)}
    Let>vDeltaMouse=TRUE
  EndIf
END>srtTestForMouseMovement

SRT>srtRunThisWhenMouseMoves
  Let>vDeltaMouse=FALSE
  GetCursorPos>vCurXold,vCurYold
  SetControlText>Macro Scheduler Message,TMemo,1,Mouse cursor IS moving
  Wait>0.2
END>srtRunThisWhenMouseMoves

Label>Loop
  If>vDeltaMouse=FALSE
    SetControlText>Macro Scheduler Message,TMemo,1,Mouse cursor is NOT moving
    Let>vDeltaMouse=STATIONARY
  EndIf
  IfWindowOpen>Macro Scheduler Message
  Else
    Exit>0
  EndIf
  Wait>0.1
Goto>Loop

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Mouse Movement Detect via Custom OnEvent>

Post by Grovkillen » Fri Oct 20, 2023 1:46 pm

Ah, nice approach! I will think about it and maybe add a "gesture recognizer" to it. I have some idea of doing it by making some orthogonal calculations and store the vectors as an array and based on the series of vectors I could interpret a gesture.
Let>ME=%Script%

Running: 15.0.27
version history

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts