Page 1 of 1

trouble with loop break

Posted: 08 Jul 2019, 05:54
by JavierBO
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]

Re: trouble with loop break  Topic is solved

Posted: 08 Jul 2019, 07:59
by Rohwedder
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

Re: trouble with loop break

Posted: 09 Jul 2019, 07:24
by JavierBO
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!

Re: trouble with loop break

Posted: 10 Jul 2019, 10:19
by eblanc
If it works, mark this as fixed.