Switching Between Loops

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Fat
Posts: 10
Joined: 11 Nov 2017, 21:03

Switching Between Loops

18 Dec 2017, 09:55

trying to get a loop to start on one key stroke, then another key stroke stop that loop and start another loop and be able to switch between them at will.
ive gotten them to go from one to the other once and then no further. the one below allows to go from one to the other but must double tap. if tapped too quickly, it gets stuck. this leads me to believe its getting stuck in the loop/delay. i dont know. ive tried a few different menial methods.. no avail.
basically looking to switch between loops. any helps or examples welcome.
thank you all.


Code: Select all

$a::
b = 1
Loop
{
if a = 1
break
Send {1}
Sleep 1000
}
a = 0
return

$b::
a = 1
Loop
{
if b = 1
break
Send {2}
Sleep 1000
}
b = 0
return
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Switching Between Loops

18 Dec 2017, 11:21

Hi.
Solving your issue with a loop is for me, as a person cheated for his education, unknown.
But a workaround is possible. Look below for a solution with a timer:

Code: Select all

Settimer, check, 1000

$a::
a = 1 
b = 0
return

$b::
a = 0
b = 1
return

check:
if (a = 1)
send 1
if (b = 1)
send 2
return

$Esc::
settimer, check, off
return
Works it as you have desired?
BTW: You don't need to "thank you all". It is enough, when you thank to me. :mrgreen: :mrgreen: :mrgreen:
Einfach nur ein toller Typ. :mrgreen:
Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Switching Between Loops

19 Dec 2017, 03:22

Hallo,
try:

Code: Select all

$a::
	b := 0, a := 1
	SetTimer a,-1
Return
a:
	While, a
	{
		Send 1
		Sleep 1000
	}
Return
$b::
	a := 0, b := 1
	SetTimer b,-1
Return
b:
	While, b
	{
		Send 2
		Sleep 1000
	}
Return
or:

Code: Select all

SetTimer, Loops, -1
$a::Loop = a
$b::Loop = b
$c::Loop =
Loops:
Loop
{
	While, Loop = "a"
	{
		Send 1
		Sleep 1000
	}
	While, Loop = "b"
	{
		Send 2
		Sleep 1000
	}
}
Return
Fat
Posts: 10
Joined: 11 Nov 2017, 21:03

Re: Switching Between Loops

20 Dec 2017, 07:25

divanebaba wrote:Hi.
Solving your issue with a loop is for me, as a person cheated for his education, unknown.
But a workaround is possible. Look below for a solution with a timer:

Code: Select all

Settimer, check, 1000

$a::
a = 1 
b = 0
return

$b::
a = 0
b = 1
return

check:
if (a = 1)
send 1
if (b = 1)
send 2
return

$Esc::
settimer, check, off
return
Works it as you have desired?
BTW: You don't need to "thank you all". It is enough, when you thank to me. :mrgreen: :mrgreen: :mrgreen:
your rendition of self has me more puzzled than i was with my little loop task now that you seem to have shown a much better angle on it. ill try this and it looks promising. we'll see once i get all the sub functions involved.
i havent taken a computer class since the 80s.. my programming logic is very rusty to say the least.
thank YOU.
Fat
Posts: 10
Joined: 11 Nov 2017, 21:03

Re: Switching Between Loops

20 Dec 2017, 07:29

Rohwedder wrote:Hallo,
try:

Code: Select all

$a::
	b := 0, a := 1
	SetTimer a,-1
Return
a:
	While, a
	{
		Send 1
		Sleep 1000
	}
Return
$b::
	a := 0, b := 1
	SetTimer b,-1
Return
b:
	While, b
	{
		Send 2
		Sleep 1000
	}
Return
or:

Code: Select all

SetTimer, Loops, -1
$a::Loop = a
$b::Loop = b
$c::Loop =
Loops:
Loop
{
	While, Loop = "a"
	{
		Send 1
		Sleep 1000
	}
	While, Loop = "b"
	{
		Send 2
		Sleep 1000
	}
}
Return
i will try both. if not used in task at hand, im sure they will be very useful in the future. i save all info and ideas i get here. problem being.... data retrieval :)
thank YOU too
xo

edit 1:
one question now is what is timer "-1" purpose?
and
is there a command to exit the loop search? like "$c::Exit"? having loop c say break or return seems to work
and
i tried the loops with one of the "sleep 1000" commands removed, and it got stuck on that loop. probably not enough time to read input
Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Switching Between Loops

21 Dec 2017, 04:30

Hallo,
timer "-1" purpose?:
If Period is negative, the timer will run only once. For example, specifying -100 would run the timer 100 ms from now then disable the timer as though SetTimer, Label, Off had been used.
is there a command to exit the loop search?
Wihout sleep?
I do not know why, but at least "Sleep, 0" is needed:

Code: Select all

SetTimer, Loops, -1
$a::Loop = a
$b::Loop = b
$c::Loop = Exit
Loops:
Loop
{
	While, Loop = "a"
	{
		SendInput 1
		Sleep, 0
	}
	While, Loop = "b"
	{
		SendInput 2
		Sleep, 0
	}
	If Loop = Exit
		Exit
}
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 177 guests