How to cancel key sending sequence when key is up

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
plookey78
Posts: 7
Joined: 08 Dec 2017, 14:40

How to cancel key sending sequence when key is up

21 Apr 2021, 15:32

Hello im new to the ahk. Im trying to make my script keep sending the sequence when key is down but cancel it when key is up and send it over again once i press the key.
I tried googling a few times for a solution but didnt found anything that works.

Code: Select all

a::
       if GetKeyState("a", "P){

                Send 5
		Random, VariableDelay, 10, 20
		Sleep, %VariableDelay%

                Send r
		Random, VariableDelay, 132, 132
		Sleep, %VariableDelay%

                Send {t down}
		Random, VariableDelay, 225, 225
		Sleep, %VariableDelay%
                Send {t up}

                Send {q down}
		Random, VariableDelay, 5000, 5000
		Sleep, %VariableDelay%
                Send {q up}

} 
	return
a UP:: 
Exit 
return
[Mod edit: [code][/code] tags added.]
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: How to cancel key sending sequence when key is up

21 Apr 2021, 18:29

This will stop after at the end of a loop iteration after you release the a key:

Code: Select all

a::
	loop {
                Send 5
		Random, VariableDelay, 10, 20
		Sleep, %VariableDelay%

                Send r
		Random, VariableDelay, 132, 132
		Sleep, %VariableDelay%

                Send {t down}
		Random, VariableDelay, 225, 225
		Sleep, %VariableDelay%
                Send {t up}

                Send {q down}
		Random, VariableDelay, 5000, 5000
		Sleep, %VariableDelay%
                Send {q up}

	} until !GetKeyState("a", "P")
return

But I'm guessing you probably want it to stop immediately upon release instead of finishing the iteration of the loop, so you could do this:

Code: Select all

a::
	loop {
                Send 5
		Random, VariableDelay, 10, 20
		Sleep, %VariableDelay%

                Send r
		Random, VariableDelay, 132, 132
		Sleep, %VariableDelay%

                Send {t down}
		Random, VariableDelay, 225, 225
		Sleep, %VariableDelay%
                Send {t up}

                Send {q down}
		Random, VariableDelay, 5000, 5000
		Sleep, %VariableDelay%
                Send {q up}

	}
return

a up::Reload
plookey78
Posts: 7
Joined: 08 Dec 2017, 14:40

Re: How to cancel key sending sequence when key is up

22 Apr 2021, 08:46

Thanks, that worked. I have another question though. So basically I want 5 4 3 r to send only once in the beginning of the keypress and then remove then from the sequence while keep sending t and q while e is being held. I tried with "keywait" but didnt really work.

Code: Select all

e::
    loop {
                Send 5
		Random, VariableDelay, 10, 20
		Sleep, %VariableDelay%
                KeyWait, 5

                Send 4
		Random, VariableDelay, 10, 20
		Sleep, %VariableDelay%
                KeyWait,4

                Send 3
		Random, VariableDelay, 10, 20
		Sleep, %VariableDelay%
                KeyWait, 3

                Send r
		Random, VariableDelay, 132, 132
		Sleep, %VariableDelay%
                KeyWait, r

                Send t
		Random, VariableDelay, 10, 30
		Sleep, %VariableDelay%

                Send q
		Random, VariableDelay, 10, 30
		Sleep, %VariableDelay%

}
	return
e up::Reload
[Mod edit: [code][/code] tags added.]
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: How to cancel key sending sequence when key is up

22 Apr 2021, 09:00

You simply put only the stuff you want to repeat inside the loop and the stuff you don't want to repeat outside the loop. Loop = repeat. KeyWait wouldn't do anything for this.

Code: Select all

e::
	Send 5
	Random, VariableDelay, 10, 20
	Sleep, %VariableDelay%

	Send 4
	Random, VariableDelay, 10, 20
	Sleep, %VariableDelay%

	Send 3
	Random, VariableDelay, 10, 20
	Sleep, %VariableDelay%
	
	Send r
	Random, VariableDelay, 132, 132
	Sleep, %VariableDelay%

	loop {

		Send t
		Random, VariableDelay, 10, 30
		Sleep, %VariableDelay%

		Send q
		Random, VariableDelay, 10, 30
		Sleep, %VariableDelay%

	}
return

e up::Reload
User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: How to cancel key sending sequence when key is up

22 Apr 2021, 09:09

Since you're doing basically the same sequence over and over, you can make the script much more concise and readable by using a function, like this:

Code: Select all

e::
	SendWithDelay(5)
	SendWithDelay(4)
	SendWithDelay(3)
	SendWithDelay("r", 132, 132)
	loop {
		SendWithDelay("t", 10, 30)
		SendWithDelay("q", 10, 30)
	}
return

e up::Reload

SendWithDelay(str, min:=10, max:=20) {
	Send, % str
	Random, r, min, max
	Sleep, r
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mapcarter, Marium0505, Rohwedder and 365 guests