Using hotkeys to switch between two loops Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Andrew1802
Posts: 23
Joined: 02 Jul 2020, 07:28

Using hotkeys to switch between two loops

02 Jul 2020, 07:45

Hello! I've been trying to make a simple script with two loops that handle mutually exclusive commands. The two loops are toggled by hotkeys and when one is toggled on, the other one SHOULD be toggled off automatically.

It looks like this:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#MaxThreadsPerHotkey 2
#SingleInstance, force

;<Some hotkeys, subroutines and one loop without a hotkey to activate it (always on, commenting this one out doesn't improve the behavior)>

Numpad2::
Toggle0 := !Toggle0
Toggle1 := 0
Tooltip
loop
{
If not Toggle0
break
Tooltip RUNNING`nLOOP1
gosub Subroutine1
}
return

Numpad3::
Toggle1 := !Toggle1
Toggle0 := 0
Tooltip
loop
{
If not Toggle1
break
Tooltip RUNNING`nLOOP2
gosub Subroutine2
}
return

;<More hotkeys...>
The script works just fine until I switch between the loops exactly 4 times, at which point the last active loop gets stuck; It won't toggle off or switch to the other one when I press the hotkey.

Any ideas?

Thank you in advance!
Last edited by Andrew1802 on 02 Jul 2020, 07:59, edited 2 times in total.
Andrew1802
Posts: 23
Joined: 02 Jul 2020, 07:28

Re: Using hotkeys to switch between two loops

02 Jul 2020, 07:55

Update: If I set "#MaxThreadsPerHotkey 12", the loops can be toggled exactly 9 times, including the initial activation.
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Using hotkeys to switch between two loops  Topic is solved

02 Jul 2020, 08:13

Hallo,
try:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force
;<Some hotkeys, subroutines and one loop without a hotkey to activate it (always on, commenting this one out doesn't improve the behavior)>
Numpad2::
IF !Loop
	SetTimer, LOOP1, -10
Loop = LOOP1
Return
LOOP1:
While, Loop = "LOOP1"
{
	Tooltip RUNNING`nLOOP1
	gosub Subroutine1
}
Goto,% Loop

Numpad3::
IF !Loop
	SetTimer, LOOP2, -10
Loop = LOOP2
Return
LOOP2:
While, Loop = "LOOP2"
{
	Tooltip RUNNING`nLOOP2
	gosub Subroutine2
}
Goto,% Loop

;<More hotkeys...>
Or shorter:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force
;<Some hotkeys, subroutines and one loop without a hotkey to activate it (always on, commenting this one out doesn't improve the behavior)>
Numpad2::
Numpad3::
;<More hotkeys...>
Loop := A_ThisHotkey "Loop"
SetTimer, Loop, 10
Return
Loop:
Goto,% Loop

Numpad2Loop:
	Tooltip RUNNING`nLOOP1
	; gosub Subroutine1
Return

Numpad3Loop:
	Tooltip RUNNING`nLOOP2
	; gosub Subroutine2
Return

;<More hotkeyLoop...>
Andrew1802
Posts: 23
Joined: 02 Jul 2020, 07:28

Re: Using hotkeys to switch between two loops

02 Jul 2020, 09:05

That works great! Thank you! I've adjusted it a bit to include toggle functionality.

Code: Select all

Numpad2::
IF !Loop OR (Loop = "EscapeLoop") OR (Loop = "LOOP2") {
	SetTimer, LOOP1, -10
	Loop = LOOP1
}
else
Loop = EscapeLoop
Return
LOOP1:
While, Loop = "LOOP1"
{
	Tooltip RUNNING`nLOOP1
}
Goto,% Loop

Numpad3::
IF !Loop OR (Loop = "EscapeLoop") OR (Loop = "LOOP1") {
	SetTimer, LOOP2, -10
	Loop = LOOP2
}
else
Loop = EscapeLoop
Return
LOOP2:
While, Loop = "LOOP2"
{
	Tooltip RUNNING`nLOOP2
}
Goto,% Loop

EscapeLoop:
ToolTip
return
My edit is likely way less elegant than what you could've come up with but hey, if it works, I'm a happy camper! :lol:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Frogrammer and 269 guests