Page 1 of 1

How to cancel key sending sequence when key is up

Posted: 21 Apr 2021, 15:32
by plookey78
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.]

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

Posted: 21 Apr 2021, 18:29
by boiler
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

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

Posted: 22 Apr 2021, 08:46
by plookey78
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.]

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

Posted: 22 Apr 2021, 09:00
by boiler
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

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

Posted: 22 Apr 2021, 09:09
by boiler
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
}