Keybind to switch between loops

Get help with using AutoHotkey and its commands and hotkeys
Phigl
Posts: 4
Joined: 17 Nov 2019, 02:52

Keybind to switch between loops

17 Nov 2019, 03:27

What I am trying to do is fairly simple. I have a few different codes that loop, and I want to switch between them using some predefined keys. What I have written is posted below.

The code in the loops works, and it mostly works as expected. If I start it up, I hit ctrl z to unpause, then nothing happens (as expected). I then hit ctrl w to start the first loop. This works, and it loops through until I either pause it again, exit the code, or press ctrl t to switch to the second loop. That also works, and it loops through until I either pause it or exit. however, I can't hit ctrl w again to go back to the first block of code.

I have the same problem if I do it in the other order. If I start up the code and press ctrl t then it executes that code on a loop. Pressing ctrl w makes it switch to the first block of code. However, I can't hit ctrl t to go back to the other code.

What has to be done differently in order to switch back and forth between them? I plan on using more than two blocks of code, so I can't just toggle, it needs to be specific keybinds.

Thank you

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Pause On

^w::
Loop 
{
...a bunch of code
}

^t::
Loop
{
...a bunch of code
}

ExitApp

^z::Pause
^x::ExitApp
Rohwedder
Posts: 3530
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Keybind to switch between loops

17 Nov 2019, 13:03

Hallo,
if your bunches of code contains special Sleeps, these Sleep() can execute the exit command:

Code: Select all

^w::
Remember() ;this in the beginning
Loop
{
	ToolTip,% A_ThisHotkey "  " A_TickCount
	Sleep(100)
}
Remember = ;this at the end
Return

^t::
Remember() ;this in the beginning
Loop
{
	ToolTip,% A_ThisHotkey "  " A_TickCount
	Sleep(100)
}
Remember = ;this at the end
Return

;-----------------------------------
Sleep(Time)
{ ;sleeps or stops the current thread and starts the new thread
	Global Remember
	End:= A_TickCount + Time
	While, (S:= End-A_TickCount) > 0
	{
		IF (Remember = A_ThisLabel)
			Sleep,S>100?100:S
		Else If Remember
		{
			SetTimer, %Remember%, -200
			Exit
		}
	}
}
Remember()
{
	Global Remember
	Exit := Remember And(Remember <> A_ThisLabel)
	Remember := A_ThisLabel
	If Exit
		Exit
	Return
}
fwejifjjwk2
Posts: 71
Joined: 10 Aug 2019, 01:49

Re: Keybind to switch between loops

17 Nov 2019, 14:15

@Rohwedder
Thank you. But why normal loop break and using if else in loop not working? Maybe we should fix this problem rather than using complx way work around.
Phigl
Posts: 4
Joined: 17 Nov 2019, 02:52

Re: Keybind to switch between loops

17 Nov 2019, 20:30

@Rohwedder thanks, it worked, but I didn't follow the logic of the code :)
User avatar
Xtra
Posts: 1923
Joined: 02 Oct 2015, 12:15

Re: Keybind to switch between loops

17 Nov 2019, 22:08

Something different:

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

^w::
^t::SetTimer, Looper, 100    ; 100ms change to suit loop time / delay

^z::Pause    ; Toggle on / off
^x::ExitApp

Looper()
{
    static lastHotKey
    if (A_ThisHotkey != "^z")
        lastHotKey := A_ThisHotkey
    
    if (A_ThisHotkey = "^w" || lastHotKey = "^w")
    {
        ToolTip, % "^w Loop " . A_Tab . A_TickCount
        ;~ ...a bunch of code
    }
    else if (A_ThisHotkey = "^t" || lastHotKey = "^t")
    {
        ToolTip, % "^t Loop " . A_Tab . A_TickCount
        ;~ ...a bunch of code
    }
}

Return to “Ask For Help”

Who is online

Users browsing this forum: blured, boiler, Gaia, Google [Bot], mikeyww, ryanburnsninja, Spikea and 52 guests