Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop an infinite loop in autohotkey using the key you start it with

So i just started using autohotkey and i made this script to spam the trade chat in a game called path of exile, it works pretty well, but i cant get it to stop when i press f1 again, i've tried countles times, but the loop just won't stop

#MaxThreads 2
wintitle=Path of Exile
SetTitleMatchMode,2
DetectHiddenWindows,On
setkeydelay,2500,0
f1::
toggle:=!toggle
Loop
{
  if toggle
    controlsend,,{enter}{up}{enter}, %wintitle%
  else
    break
}
return
like image 562
Steve TheDistraction Avatar asked Dec 04 '22 05:12

Steve TheDistraction


1 Answers

I think you're better off using SetTimer for this. Loops aren't very easy to work with when it comes to toggles.

i := 0
toggle := 0
F1::
    toggle := !toggle
    if (toggle) {
        SetTimer, Timer_Spam, 10
    } else {
        SetTImer, Timer_Spam, Off
    }
return

Timer_Spam:
    TrayTip, Counter, %i%
    i++
return

The reason why your loop isn't working is because once you enter the loop the program is stuck there, so to get out you need to work from inside the loop.

You can do this with GetKeyState(), but then you can't use the same key to toggle it on and off, as it'll toggle off as soon as you start it, unless you add Sleep commands in there, in which case it becomes unreliable instead.

You can however use a separate key to stop the loop, shown here.

toggle := 0
i := 0
F1::
    toggle := !toggle
    if (toggle) {
        Loop {
            if (GetKeyState("F2", "P")) {
                toggle := !toggle
                break
            }

            TrayTip, Counter, %i%
            i++
        }   
    }
return

But like I said above, SetTimer achieves the same result in a much more stable way. So I'd go with that.

like image 150
Sid Avatar answered Feb 01 '23 23:02

Sid