Search code examples
autohotkey

Unstoppable script Issue


I have a script which uses several mousemoves and, when needed, I press F3 to stop the moves forcing a reload or ESC to stop the moves exiting the app. It works fine with no issues in the regular speed.

The problem is: when I decrease the mousespeed and increase the mousedelay, the script becomes unstoppable: I press the F2, F3 and ESC keys several times (trying to make it stops) but the script ignores it and continues running the mousemoves.

Here's a minimal example of my problem:

SendMode Input

$F1::
SetDefaultMouseSpeed, 50
SetMouseDelay, 30

Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600
Mousemove, 200, 200
Mousemove, 600, 600

SoundPlay, *48
return

$F2:: Pause
$F3:: Reload
$ESC:: ExitApp

Why does it happens and how can I fix it (how can I stop the script even with low speed + high delay)? p.s.: F1 key starts the mousemoves sequence.


Solution

  • MouseMove with non-zero speed cannot be interrupted by another hotkey inside the same AHK script because Autohotkey is single-threaded internally (it just emulates thread-like behavior) and performs a blocking sleep for each mouse micromovement until it reaches the overall distance you specified.

    Solutions:

    • Check whether a hotkey was pressed after each MouseMove, but it's non-instantaneous;
    • Implement your own MouseMove that checks GetKeyState after each 1px movement;
    • Use a master script with hotkeys that run/pause/stop the child:

      Stopping: trivial, use Process Close
      Running: the child should hide tray icon otherwise it'll stay after process close
      Pausing: nontrivial but there are solutions (see also the underlying mechanics).