loop break not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

loop break not working

Post by nina1000 » 24 Jun 2022, 08:05

Dear all,

please help me out of this - what did I do wrong that my script is not working.
Goal is, the loop sends randomly 1,2,3,4,5. It shall start by manually pressing "Q" and shall stop again when pressing "W"

Code: Select all

#NoEnv
#Warn
#SingleInstance Force
SendMode Input
SetWorkingDir %A_ScriptDir%

-----------------------------------------------------------------------------------------

Q::
active:=1
if(active=0){
	break 
}

RandomFire()

RandomFire() {
	
	static AllKeys := ["1", "2", "3", "4", "5"]
	keys := AllKeys.Clone()
	loop % AllKeys.Length()
	{
		Random n, 1, % keys.Length()
		key := keys.RemoveAt(n)
		
		send {%key% down}
		Random sleep1, 281, 993
		Sleep %sleep1%
		send {%key% up}
	}
}

W::
active:=0
return

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

Re: loop break not working

Post by Rohwedder » 24 Jun 2022, 08:20

Hallo,
currently (when the active-lines are deleted) it stops by itself after 5 numbers. Should it be able to stop within this cycle?
Last edited by Rohwedder on 24 Jun 2022, 08:26, edited 1 time in total.

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: loop break not working

Post by nina1000 » 24 Jun 2022, 08:25

Hi, it shall stop when pressing manually ´W´ (not after 5 loops) - if not pressed ´W´ loop shall continue continously.

(Hallo, es soll mit ´Q´starten, dann endlos auf Zufallsbasis 1,2,3,4, oder 5 drücken, und zwar so lange wiederholen, bis ich es mit ´W´stoppe) :crazy:
Last edited by nina1000 on 24 Jun 2022, 08:31, edited 1 time in total.

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

Re: loop break not working  Topic is solved

Post by Rohwedder » 24 Jun 2022, 08:31

This?:

Code: Select all

#NoEnv
#Warn
#SingleInstance Force
SendMode Input
SetWorkingDir %A_ScriptDir%

-----------------------------------------------------------------------------------------

Q::
Global active := 1
RandomFire()
Return
RandomFire() {	
	static AllKeys := ["1", "2", "3", "4", "5"]
	keys := AllKeys.Clone()
	loop % AllKeys.Length()
	{
		Random n, 1, % keys.Length()
		key := keys.RemoveAt(n)
		
		send {%key% down}
		Random sleep1, 281, 993
		Sleep %sleep1%
		send {%key% up}
	}
	Until, !active
}
W::
active:=0
return
or that?:

Code: Select all

#NoEnv
#Warn
#SingleInstance Force
SendMode Input
SetWorkingDir %A_ScriptDir%

-----------------------------------------------------------------------------------------

Q::
Global active := 1
While, active
	RandomFire()
Return
RandomFire() {	
	static AllKeys := ["1", "2", "3", "4", "5"]
	keys := AllKeys.Clone()
	loop % AllKeys.Length()
	{
		Random n, 1, % keys.Length()
		key := keys.RemoveAt(n)
		
		send {%key% down}
		Random sleep1, 281, 993
		Sleep %sleep1%
		send {%key% up}
	}
	Until, !active
}
W::
active:=0
return

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: loop break not working

Post by nina1000 » 24 Jun 2022, 08:36

perfect! "or that" works as expected!
can you please explain what and how you fixed it?

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

Re: loop break not working

Post by Rohwedder » 24 Jun 2022, 09:12

I removed the break that was not inside a loop.
Made active global so that it is also readable within a function.
Created an outer While loop which ends at active = 0.
Added an Until to the inner loop in RandomFire() which also ends that loop at active = 0.

User avatar
nina1000
Posts: 16
Joined: 30 Mar 2022, 13:02

Re: loop break not working

Post by nina1000 » 24 Jun 2022, 09:14

Thank you so much for your help, appreciated!

Post Reply

Return to “Ask for Help (v1)”