Creating a toggle with a complex loop

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Creepyfishwoman
Posts: 1
Joined: 30 Apr 2024, 22:59

Creating a toggle with a complex loop

Post by Creepyfishwoman » 30 Apr 2024, 23:11

Hello! I am trying to create a script that automatically sends a loop of inputs that can be toggled on and off. The loop of inputs I have down, but I cannot get the toggle to work and all documentation I have tried fails to work with my script. How would I be able to create a toggle with the F7 key with this loop? Thank you.

Code: Select all

#SingleInstance Force
#MaxThreadsperHotkey 2

SendMode "Input"

^F7:: {
	static toggle := false
	
	If toggle := !toggle
		{
		Loop 
			{
			Send ("{m down}")
				sleep 50
				Send ("{m up}")
				sleep 500
			Loop 3
				{
					sleep 1000
					Send ("{up down}")
					sleep 50
					Send ("{up up}")
						}
				sleep 1000
				Send ("{Enter down}")
				sleep 50
				Send ("{Enter up}")
				Sleep 500
				Send ("{right down}")
				sleep 50
				Send ("{right up}")
				sleep 3000
				Send ("{left down}")
				sleep 50
				Send ("{left up}")
				Sleep 500
				Loop 9
				{
					sleep 30
					Send ("{down down}")
					sleep 50
					Send ("{down up}")
					sleep 1000
						}
			Sleep 1000
				Send ("{Backspace down}")
				sleep 50	
			Send ("{Backspace up}")
				sleep 1000
				Send ("{Backspace down}")
				sleep 50
				Send ("{Backspace up}")
				sleep 5000
				Send ("{m down}")
				sleep 50
				Send ("{m up}")
				sleep 500
			Loop 3
				{
					sleep 1000
					Send ("{up down}")
					sleep 50
					Send ("{up up}")
						}
				sleep 1000
				Send ("{Enter down}")
				sleep 50
				Send ("{Enter up}")
			}
			
		}

	
}

User avatar
boiler
Posts: 17146
Joined: 21 Dec 2014, 02:44

Re: Creating a toggle with a complex loop

Post by boiler » 01 May 2024, 01:43

It toggles the value of the variable toggle, but you have nothing once inside the loop that causes it to stop when its value changes, so the second instance of the hotkey function interrupts the first instance and changes the value of toggle, then control is returned to the first instance where it continues executing inside the loop with no reason to stop. You would either need to be checking the value of toggle inside the loop at whatever points you’d like and break the loop or return from the function, or even easier would be to Reload the script when the value of toggle is true (starting with a true value so it becomes true again on the second press) so it will stop executing the loop no matter what point it is in the loop:

Code: Select all

#SingleInstance Force
#MaxThreadsperHotkey 2

SendMode "Input"

^F7:: {
	static toggle := true
	
	If toggle := !toggle
		Reload
	Loop 
		{
		Send ("{m down}")
			sleep 50
			Send ("{m up}")
			sleep 500
		Loop 3
			{
				sleep 1000
				Send ("{up down}")
				sleep 50
				Send ("{up up}")
					}
			sleep 1000
			Send ("{Enter down}")
			sleep 50
			Send ("{Enter up}")
			Sleep 500
			Send ("{right down}")
			sleep 50
			Send ("{right up}")
			sleep 3000
			Send ("{left down}")
			sleep 50
			Send ("{left up}")
			Sleep 500
			Loop 9
			{
				sleep 30
				Send ("{down down}")
				sleep 50
				Send ("{down up}")
				sleep 1000
					}
		Sleep 1000
			Send ("{Backspace down}")
			sleep 50	
		Send ("{Backspace up}")
			sleep 1000
			Send ("{Backspace down}")
			sleep 50
			Send ("{Backspace up}")
			sleep 5000
			Send ("{m down}")
			sleep 50
			Send ("{m up}")
			sleep 500
		Loop 3
			{
				sleep 1000
				Send ("{up down}")
				sleep 50
				Send ("{up up}")
					}
			sleep 1000
			Send ("{Enter down}")
			sleep 50
			Send ("{Enter up}")
		}
}

By the way, the default Send mode in v2 is Input, so you don’t need the SendMode line. Also, you said you want to toggle with F7, but your script indicates Ctrl+F7 as the hotkey.

Post Reply

Return to “Ask for Help (v2)”