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.