Trying to break a loop with e.g. ctrl+key but it doesn't work, any options? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MusicalMethuselah
Posts: 3
Joined: 17 Nov 2022, 14:47

Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by MusicalMethuselah » 17 Nov 2022, 15:00

Hello, I'm making just a simple clicker code like so:

Code: Select all

^+!0::
KeyWait Alt
KeyWait Control
KeyWait Shift
ToggleThis := !ToggleThis
Loop, 1000 {
	if not ToggleThis
		break
	Send {LButton}
	Sleep 20
}
return
So hopefully if I do ctrl+alt+shift+0, it should start and stop a quick clicker. The problem is that it can start, but trying the hotkey again will not stop the loop. It basically has to wait for the loop to finish before I can press ctrl+alt+shift+0 again. Of course, that time it sets ToggleThis to false, so I have to press it one more time and then it will just run the loop again.

I have also tried:

Code: Select all

^+!9::
  Toggle := !Toggle
  If Toggle
    SetTimer, Trigger, -1
return

Trigger:
While (Toggle)
  {
    Send {LButton}
	Sleep 20
  }
Return
In the hopes that the while loop would just keep going and I could press ctrl+alt+shift+9 to stop or start it. It still doesn't work, though: once the loop is started, I can't stop it and have to ExitApp.

However, if the starting and stopping code is a single key, it works just fine:

Code: Select all

NumpadDiv::
StopLoop = 1
return

NumpadMult::
StopLoop = 0
return

^+!8::
; KeyWait Alt
; KeyWait Control
; KeyWait Shift
Loop, 1000 {
	if (StopLoop = 1)
		break
	Send {LButton}
	Sleep 20
}
return
I can press the keys to stop the loop instantly without exitingApp and then just reset the variable and start it again. I just want to do this not using up any base key presses: I'd much rather hold ctrl and/or alt and/or shift when pressing these. Is there any way to have this work? I'd love to have:

Code: Select all

^+!NumpadDiv::
StopLoop = 1
return

^+!NumpadMult::
StopLoop = 0
return

^+!8::
; KeyWait Alt
; KeyWait Control
; KeyWait Shift
Loop, 1000 {
	if (StopLoop = 1)
		break
	Send {LButton}
	Sleep 20
}
return
But right now it does not. Any help would be appreciated. Thanks!

picklepissjar
Posts: 20
Joined: 22 Oct 2022, 10:03

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by picklepissjar » 17 Nov 2022, 15:09

Code: Select all

v::
{
	Togg := !Togg
	SetTimer, trig, -20
	Return
}
trig:
If(Togg)
{
	Send {LButton}
	SetTimer, trig, -20
}	
Return
I think this does what you want (I changed the key to v for testing)
Edit: Changed to negative so the timer doesnt run continuously. oops :P

RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by RussF » 17 Nov 2022, 15:25

@picklepissjar, you edited it while I was typing. I was wondering why you set the same timer twice. I'm still not sure why you set it to a negative value. How about:

Code: Select all

v::
{
	Togg := !Togg
	SetTimer, trig, 20
	Return
}
trig:
If(Togg)
	Send {LButton}	; or Click
Else
	SetTimer, trig, OFF	
Return
Russ

picklepissjar
Posts: 20
Joined: 22 Oct 2022, 10:03

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by picklepissjar » 17 Nov 2022, 15:36

@RussF That version is better :lol:
Still learning stuff myself.

MusicalMethuselah
Posts: 3
Joined: 17 Nov 2022, 14:47

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by MusicalMethuselah » 17 Nov 2022, 16:17

For both @RussF and @picklepissjar , thank you for trying, but adding any modifiers to the key "v" in both of your examples doesn't work, as I had explained in my original post. My original code would work just like yours if the hotkey was just a single key, but the moment I try to add a modifier to it I can't break the loop. If you can get "^v" or anything similar to work, please let me know how.

picklepissjar
Posts: 20
Joined: 22 Oct 2022, 10:03

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?  Topic is solved

Post by picklepissjar » 17 Nov 2022, 16:36

Code: Select all

$^+!0::
This worked for me. (I guess I skimmed too fast) :P

MusicalMethuselah
Posts: 3
Joined: 17 Nov 2022, 14:47

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by MusicalMethuselah » 17 Nov 2022, 18:23

@picklepissjar this is perfect! Worked like a charm! Thank you!!

My full code now looks like this, thanks Russ:

Code: Select all

$^+!9::
{
  Toggle1 := !Toggle1
  SetTimer, Trigger, 20
  return
}

Trigger:
If (Toggle1)
    Send {LButton}
  Else
    SetTimer, Trigger, OFF
Return

RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Trying to break a loop with e.g. ctrl+key but it doesn't work, any options?

Post by RussF » 18 Nov 2022, 06:24

An even shorter version (I knew there had to be one!)

Code: Select all

$^+!9::SetTimer, Trigger, % (Toggle1 := !Toggle1) ? 20 : "OFF"

Trigger:
    Send {LButton}
Return
Russ

Post Reply

Return to “Ask for Help (v1)”