Toggle a Loop if a certain value is toggled

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
NinjoOnline
Posts: 5
Joined: 24 Apr 2024, 01:25
Contact:

Toggle a Loop if a certain value is toggled

24 Apr 2024, 03:28

I'm trying to run a loop, where when I press F1, it starts a loop of key binds and will continue said loop until I press F1 again (to toggle off) but at the moment, it's always exiting the loop instantly?

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

; Create gui display
MyGui := Gui(, "Secret Key")
MyGui.SetFont("s12")
MyGui.Add("Text", "x0 w200 Center", "Press F1 to toggle")
MyGui.Add("Text", "Right w100", "Active: ")
MyGui.Add("Text", "x+0 yp cRed vactive", "FALSE")
MyGui.Show("x1700 y920 w200 h80 NoActivate")

; Variable to control loop
LoopControl := false

LoopFunction()
{
    Loop
    {
        if (LoopControl)
        {
            ; Press D for 0.25 seconds
            Send "{d down}"
            Sleep(250)
            Send "{d up}"
            
            ; Click on a specific point on the screen
            ;Click, 100, 100  ; Change the coordinates to your specific point
            
            ; Wait for 0.25 seconds
            Sleep(250)
            
            ; Press D for 0.25 seconds
            Send "{d down}"
            Sleep(250)
            Send "{d up}"
            
            ; Press W for 1.5 seconds
            Send "{W down}"
            Sleep(1500)
            Send "{W up}"
            
            ; Click on a specific point on the screen
            ;Click, 200, 200  ; Change the coordinates to your specific point
            
            ; Wait for 2 seconds
            Sleep(2000)
        }
        else
        {
            Break  ; Terminates the loop
        }
    }
}

; F1 toggle keybind
F1:: {
    static Toggle := false

    if Toggle := !Toggle {
        LoopControl := true

        MyGui["active"].Opt("cGreen")
        MyGui["active"].Text := "TRUE"
    }
    else {
        LoopControl := false

        MyGui["active"].Opt("cRed")
        MyGui["active"].Text := "FALSE"
    }
}
NinjoOnline
Posts: 5
Joined: 24 Apr 2024, 01:25
Contact:

Re: Toggle a Loop if a certain value is toggled

24 Apr 2024, 05:05

Draken wrote:
24 Apr 2024, 04:28
Add a hotkey to pause the script?

https://www.autohotkey.com/docs/v2/lib/Pause.htm
Not really what I'm after. I'm trying to get the specific section of code to loop
NinjoOnline
Posts: 5
Joined: 24 Apr 2024, 01:25
Contact:

Re: Toggle a Loop if a certain value is toggled

24 Apr 2024, 05:15

I managed to figure it out :)

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
#MaxThreadsPerHotkey 2

ConfirmX := 100
ConfirmY := 100

; Create gui display
MyGui := Gui(, "Secret Key")
MyGui.SetFont("s12")
MyGui.Add("Text", "x0 w200 Center", "Press F1 to toggle")
MyGui.Add("Text", "Right w100", "Active: ")
MyGui.Add("Text", "x+0 yp cRed vactive", "FALSE")
MyGui.Show("x1700 y920 w200 h80 NoActivate")

; F1 toggle keybind
F1:: {
    static Toggle := false

    Toggle := !Toggle
    if Toggle {
        MyGui["active"].Opt("cGreen")
        MyGui["active"].Text := "TRUE"

        While (Toggle) {
            if not Toggle {
                Break
            }
            
            ; Press D for 0.25 seconds
            Send "{d down}"
            Sleep(250)
            Send "{d up}"
            
            Click ConfirmX, ConfirmY ; Click confirmation to open door
            
            Sleep(500) ; Wait for door to open
            
            ; Press D to walk towards and enter door
            Send "{d down}"
            Sleep(500)
            Send "{d up}"
            
            ; Press W to walk towards the booth
            Send "{W down}"
            Sleep(2000)
            Send "{W up}"
            
            ; TODO Figure out how to read screen and choose best option?
            ; Click on a specific point on the screen
            ;Click, 200, 200  ; Change the coordinates to your specific point
            
            Sleep(4000) ; Wait for transition back to outside
        }
    }
    else {
        MyGui["active"].Opt("cRed")
        MyGui["active"].Text := "FALSE"
    }
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Dazzer123, jackman, mikeyww, niCode, Viola, Yahoo [Bot] and 28 guests