Help with creating script: Toggle GUI indicator (On/off)

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Peiya
Posts: 1
Joined: 26 Apr 2024, 10:48

Help with creating script: Toggle GUI indicator (On/off)

26 Apr 2024, 11:00

Hello everyone! Could someone help me with finishing my script? My goal is to create a script that presses a button every x to y seconds (random delay) on a specific window, with a toggle to turn the script on/off (and one to exit the script), and a GUI image to indicate if it is active or not. I've managed to "write" up to the part of pressing the button, but the GUI doesn't seem to work. Here is what I have so far:

Code: Select all

#Requires AutoHotkey v2.0
#MaxThreadsPerHotkey 2

pic1 := 'HBITMAP:*' LoadPicture("PathToOnfIndicator")
pic2 := 'HBITMAP:*' LoadPicture("PathToOffIndicator")
statusGui := Gui("+AlwaysOnTop -Caption")
pic := statusGui.AddPic("w50 h-1", pic2)
statusGui.Show

ExitButton::ExitApp

ToggleButton::
{
        static toggle := 0
        toggle := !toggle
        if toggle
	    {
                pic.Value := pic1
                Loop
                {
                ControlSend("ButtonPress",, "ahk_exe window.exe")
                delay := Random(x, y)
                sleep delay
                }
        }
        else
        {
        pic.Value := pic2
        }
}
Some problems I have run into so far:

- The script works 3 times, then stays with the On indicator forever
- It might even not work without a reload at the else statement
- After a few changes, I managed to make it kinda work, but there's a delay from On to Off, as if it's waiting for the sleep to finish

I still haven't positioned the indicator yet, and it still has a white background instead of a transparent one. I'm assuming I can do these two things (maybe wrong) and would like to resolve this problem first. Sorry if the code is messy/inefficient, I pretty much just stitched a few examples together, since I suck at code. Any help would be appreciated!
User avatar
mikeyww
Posts: 27131
Joined: 09 Sep 2014, 18:38

Re: Help with creating script: Toggle GUI indicator (On/off)

26 Apr 2024, 13:58

If you trigger a hotkey a second time, it does not abort the first subroutine. Instead, it interrupts it. When the interrupting routine ends, the interrupted routine will resume.

I did not test your script, but having an infinite loop within the subroutine complicates it. Nothing will completely break the loop permanently unless you add code to do so.

I would start over, and build and test one step at a time. Can you make a toggle? Can the toggle do something simple? Etc. After it works, add a GUI.

SetTimer would be a simple way to start and stop an action.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, mikeyww and 52 guests