Archeg
Archeg

Reputation: 8462

Toggle infinite loop by the same key

I want to be able to press key and run an infinite loop, and be able to press the same key to stop the loop. In a simple case I want the script to click indefinitely:

XButton2::
if (Doing = 0)
{
  Doing := 1
  While Doing = 1
  {
     Click
  }
  return
} else {
 Doing := 0
}

Unfortunately this doesn't work - XButton2 does not respond any more because it is running a script. I can switch Doing := 0 by other key but I want for it to be the same XButton2. Any simple way of doing this? I would avoid complicated solutions.

Upvotes: 1

Views: 1299

Answers (1)

phil294
phil294

Reputation: 10872

Use a timed subroutine. That's how you'd start/stop a loop in ahk usually.

For example:

clicking := false

label_click:
    click
return

XButton2::
    clicking := !clicking
    if(clicking)
        setTimer, label_click, 1 ; click once every millisecond (approx.)
    else
        setTimer, label_click, off
return

That is how I usually do it, though I'd love to see a more compact version.

Upvotes: 2

Related Questions