trouble with loop break Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JavierBO
Posts: 14
Joined: 08 Jul 2019, 05:43

trouble with loop break

08 Jul 2019, 05:54

I'm having some trouble with the break in the following code.

Code: Select all

SetMouseDelay -1,-1,-1
SetKeyDelay -1,-1,-1
SetDefaultMouseSpeed, 0
#maxhotkeysperinterval 99999
#MaxThreadsPerHotkey 3

PgUp::
#MaxThreadsPerHotkey 1
	if KeepWinZRunning
{
KeepWinZRunning := false
    return
}
; Otherwise:
KeepWinZRunning := true
Loop
{
		
		Send, {Home}
		Sleep, 2025
		Send, {Ins}
		Sleep, 2025
		Send, {Del}
		Sleep, 2025
			
		if not KeepWinZRunning
       break
    
}
KeepWinZRunning := false
return
The script works almost perfectly for what i want but there's one part that is troubling me.
After pressing Page Up a second time after starting the Loop the loop only breaks after running every send(home,ins,del) instead of breaking whenever I press Page Up.
Is there a way to stop the script as soon as my set key is pressed instead of only breaking after the whole loop finishes?
I know it can be done with if not GetKeyState() but i really don't want to be holding the key for the script to work.

[Mod edit: Topic name added]
Rohwedder
Posts: 7768
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: trouble with loop break  Topic is solved

08 Jul 2019, 07:59

Hallo,
try:

Code: Select all

SetMouseDelay -1,-1,-1
SetKeyDelay -1,-1,-1
SetDefaultMouseSpeed, 0
#maxhotkeysperinterval 99999
#MaxThreadsPerHotkey 2
PgUp::
#MaxThreadsPerHotkey 1
IF !KeepWinZRunning:=!KeepWinZRunning
	Return
Loop
{
	Send, {Home}
	Sleep(2025,KeepWinZRunning)
	Send, {Ins}
	Sleep(2025,KeepWinZRunning)
	Send, {Del}
	Sleep(2025,KeepWinZRunning)
}
return
Sleep(Time,ByRef On:=True)
{ ;like "Sleep, Time", but if On becomes Zero, the Thread ends
    End:= A_TickCount + Time - 10
    While, S:= End-A_TickCount > 0
        IF On
            Sleep,S>100?100:S
        Else Exit
} ;a long Sleep will be segmented to be able to interrupt it fast
JavierBO
Posts: 14
Joined: 08 Jul 2019, 05:43

Re: trouble with loop break

09 Jul 2019, 07:24

Rohwedder wrote:
08 Jul 2019, 07:59
Hallo,
try:

Code: Select all

SetMouseDelay -1,-1,-1
SetKeyDelay -1,-1,-1
SetDefaultMouseSpeed, 0
#maxhotkeysperinterval 99999
#MaxThreadsPerHotkey 2
PgUp::
#MaxThreadsPerHotkey 1
IF !KeepWinZRunning:=!KeepWinZRunning
	Return
Loop
{
	Send, {Home}
	Sleep(2025,KeepWinZRunning)
	Send, {Ins}
	Sleep(2025,KeepWinZRunning)
	Send, {Del}
	Sleep(2025,KeepWinZRunning)
}
return
Sleep(Time,ByRef On:=True)
{ ;like "Sleep, Time", but if On becomes Zero, the Thread ends
    End:= A_TickCount + Time - 10
    While, S:= End-A_TickCount > 0
        IF On
            Sleep,S>100?100:S
        Else Exit
} ;a long Sleep will be segmented to be able to interrupt it fast
Perfect thanks!
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Re: trouble with loop break

10 Jul 2019, 10:19

If it works, mark this as fixed.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 354 guests