About toggle loops

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Daneszkk
Posts: 3
Joined: 27 May 2023, 10:26

About toggle loops

Post by Daneszkk » 27 May 2023, 10:35

Hey,
I want to make a script that holds down keys for a given time, 3 different actions totally, and it loops them forever. I managed to write that, it works perfectly fine. The problem is, that i cannot toggle the loop, neither break, pause it, or even exit the program. What happens is, the keys that its still holding down, keep being held down until i press them once. What I want is that one button would toggle the loop, meaning it would start it, second press would either pause, or break. I specifically want the keys to stop being held down the moment i toggle the loop. I've tried at least 5 different methods, but the keys kept being held down, even after putting SendInput {key up}, or anything similar.
I am totally a beginner, so any help would be appreciated, as I have no clue how to solve this. Here's the loop i want to toggle with one button:

Code: Select all

Loop
	{
	SendInput {d down}{LButton down}
	sleep 122000
	SendInput {d up}{a down}{LButton down}
	sleep 122000
	SendInput {a up}{d down}{LButton down}
	sleep 130000
	SendInput {d up}{LButton up}
	}

[Mod action: Moved topic to v1 section since this is v1 code. The main section is for v2.]

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: About toggle loops

Post by Rohwedder » 27 May 2023, 12:57

Hallo,
try:

Code: Select all

Loop
	{
	SendInput {d down}{LButton down}
	sleep 122000
	SendInput {d up}{a down}{LButton down}
	sleep 122000
	SendInput {a up}{d down}{LButton down}
	sleep 130000
	SendInput {d up}{LButton up}
	}
q:: ;releases the keys and exits the script
SendInput {a up}{d up}{LButton up}
ExitApp
or something like this:

Code: Select all

q:: ;Loop on
QLoop := True
Loop
{
	SendInput {d down}{LButton down}
	Sleep(122000, QLoop)
	SendInput {d up}{a down}{LButton down}
	Sleep(122000, QLoop)
	SendInput {a up}{d down}{LButton down}
	Sleep(130000, QLoop)
	SendInput {d up}{LButton up}
}
w::QLoop := False ; Loop off
w Up::SendInput {a up}{d up}{LButton up}

Sleep(Time,ByRef Active:=True)
{ ;like "Sleep, Time", but if Active becomes False, the Thread ends
	End:= A_TickCount + Time
	While, S:= End-A_TickCount > 0
		IF Active
			Sleep,S>100?100:S
	Else Exit
} ;a long Sleep will be segmented to be able to interrupt it fast

Daneszkk
Posts: 3
Joined: 27 May 2023, 10:26

Re: About toggle loops

Post by Daneszkk » 27 May 2023, 15:48

Thank you so much, the second is almost perfect. I wonder if its possible to pause the loop at a state it's currently at, and when started again, it would continue right where it paused. I figured sleep might have to be changed to timer, but i have no clue how to set it up.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: About toggle loops

Post by Rohwedder » 28 May 2023, 01:26

Then try:

Code: Select all

Pause
Loop
{
	SendInput {d down}{LButton down}
	sleep 122000
	SendInput {d up}{a down}{LButton down}
	sleep 122000
	SendInput {a up}{d down}{LButton down}
	sleep 130000
	SendInput {d up}{LButton up}
}
q::Pause ;Loop on/off
or:

Code: Select all

Pause
Loop
{
	SendInput,% Keys := "{d down}{LButton down}"
	sleep 122000
	SendInput,% Keys := "{d up}{a down}{LButton down}"
	sleep 122000
	SendInput,% Keys := "{a up}{d down}{LButton down}"
	sleep 130000
	SendInput,% Keys := "{d up}{LButton up}"
}
q:: ;Loop on/off, Keys released during the pause
IF A_IsPaused
	SendInput,% Keys
Else
	SendInput {a up}{d up}{LButton up}
Pause,,1
Return
Pause does stop the entire script, i.e. this .ahk file, but what's wrong with running multiple scripts at once?

Daneszkk
Posts: 3
Joined: 27 May 2023, 10:26

Re: About toggle loops

Post by Daneszkk » 30 May 2023, 15:38

Is it possible to pause the sleep state aswell? Like if the first action is say 40 seconds in, and if i pause, it will do the remaining part only? As i tested with a few seconds, the sleep state wouldn't pause.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: About toggle loops

Post by Rohwedder » 31 May 2023, 00:21

Autohotkey does not have something like a sleep state. If you command it to sleep then the script does not really sleep but waits the time commanded before continuing the execution of that threat. Another thread can interrupt this idleness but cannot take back the sleep command of the other threat.
(What boss orders his subordinate to sleep during working hours?!)
My main script currently has 18619 lines. Except for 3 sleeps that are waiting for web pages to load, all are <= 1000 ms.
Instead of long sleeps, I use lots of timers with different priorities and a few temp-triggered subscripts started by the main script.

Post Reply

Return to “Ask for Help (v1)”