Steve TheDistraction
Steve TheDistraction

Reputation: 41

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

Upvotes: 4

Views: 29014

Answers (6)

Sid
Sid

Reputation: 1719

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.

Upvotes: 6

vasili111
vasili111

Reputation: 6940

This code does what you want:

#MaxThreads 2
wintitle=Path of Exile
SetTitleMatchMode,2
DetectHiddenWindows,On
setkeydelay,2500,0

return
F1::
    Loop
    {

        CheckLButton1:
       if (GetKeyState("F1"))
        {
            Goto, CheckLButton1
        }

        Docode:
        controlsend,,{enter}{up}{enter}, %wintitle%
        ;ToolTip, 1


        if (!(GetKeyState("F1")))
        {
            Goto, Docode
        }

        CheckLButton2:
       if (!(GetKeyState("F1")))
        {
            return
        }
        else
        {
            Goto, CheckLButton2
        }
    }
return

If you need explanation, look here at my post: http://ahkscript.org/boards/viewtopic.php?f=5&t=4613#p26298

Upvotes: 1

dalslandan200
dalslandan200

Reputation: 59

This is the easiest approach I was able to do.

Start/stop toggle with key "2", sending "a" with 0.1 second delay.

#MaxThreadsPerHotkey 2

running := false
stop := false

~2::

if(!running) {
    running := true
}
else {
    stop := true
    return
}

loop {

    Send {a} ; example sending key "a"

    if(stop) {
        running := false
        stop := false
        break
    }

    Sleep, 100

}

return

Upvotes: 3

Polar Bear
Polar Bear

Reputation: 31

Working example with Loop command. Yet so simple.

#Persistent
#MaxThreadsPerHotkey 2
toggle := False

f1 UP::
toggle := !toggle

Loop {
    If (!toggle) {
        break
    }
    ; Spam commands here
}
Return

Upvotes: 3

FAM0R
FAM0R

Reputation: 21

My strategy is this, Use this command:

 v::
loop
{
click
if (GetKeyState("b")) {
break
    }
}
return

(Its simple AutoClicker)

Upvotes: 2

alpha bravo
alpha bravo

Reputation: 7948

use MaxThreadsPerHotkey

#MaxThreadsPerHotkey 2
wintitle=Path of Exile
SetTitleMatchMode,2
DetectHiddenWindows,On
setkeydelay,2500,0
return

f1::
toggle:=!toggle
Loop
{
if toggle
controlsend,,{enter}{up}{enter}, %wintitle%
else
break
}
return

Upvotes: 3

Related Questions