Press a key to stop loop Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Jeanali
Posts: 3
Joined: 24 Sep 2022, 07:02

Press a key to stop loop

Post by Jeanali » 24 Sep 2022, 07:19

Hello,
I am new to autohotkey,
I have a script loop that presses a key for 10 seconds, I want to be able to press a different key and stop the loop
this is my script:

Code: Select all

Loop
{
 Send, {SPACE Down}
 Sleep 10000
 Send, {SPACE Up}
 sleep 25
}
return

^k::Pause

~F3::ExitApp

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

Re: Press a key to stop loop  Topic is solved

Post by Rohwedder » 24 Sep 2022, 09:05

Hallo,
try:

Code: Select all

Stop := False
Loop
{
	Send, {SPACE Down}
	Loop, 100
	{
		IF Stop
			Break 2
		Else Sleep, 100
	}
	Send, {SPACE Up}
	sleep 25
}
Send, {SPACE Up}
SoundBeep
return

q::Stop := True ;Press key Q to break loop

^k::Pause

~F3::ExitApp
or:

Code: Select all

Loop
{
	Send, {SPACE Down}
	KeyWait, q, Down T10
	IF !ErrorLevel
		Break 
	Send, {SPACE Up}
	sleep 25
}
Send, {SPACE Up}
SoundBeep
return

q::Return ;Press key Q to break loop

^k::Pause

~F3::ExitApp

Jeanali
Posts: 3
Joined: 24 Sep 2022, 07:02

Post by Jeanali » 24 Sep 2022, 12:39

Thanks, it works.
Another thing, I want to be able to toggle the loop.

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

Re: Press a key to stop loop

Post by Rohwedder » 25 Sep 2022, 01:29

You would like to be able to toggle as well?
At the latest then you should say goodbye to these stupid Loops and turn to Timers!
Toogle Timer TSpace on/off with key Q:

Code: Select all

q::SetTimer, TSpace,% (TSpace:=!TSpace)?10000:"Off"
q UP::Send,% TSpace?"{SPACE Down}":"{SPACE Up}"
TSpace:
Send, {SPACE Up}
sleep 25
Send, {SPACE Down}
Return
The Timer concept is admittedly more demanding to master but significantly more powerful especially when multiple simultaneous tasks are required.

Post Reply

Return to “Gaming Help (v1)”