Toggle hold a key down until a DIFFERENT key is released. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
kain067
Posts: 5
Joined: 04 May 2021, 07:36

Toggle hold a key down until a DIFFERENT key is released.

Post by kain067 » 04 May 2021, 07:47

Hello helpful people, most scripts I'm able to figure out from searching but this is a real tough one.

I want to toggle holding a key down (q) by pressing it while I'm already physically holding a different key down (w), and then untoggle (release) that key (q) when I let go of the key I'm actually holding down (w).

So I'll be already holding W down, then I want to just press Q to toggle holding down Q, but then Q releases itself when I let go of W.

This is to enable toggling of a run/sprint key in an FPS, but not have to press that key again to untoggle it (the sprint will be disabled when I stop moving forward). This mimics the toggle sprint behavior of games that natively support it (the game I'm playing now does not offer sprint toggle).

I know the following code works for toggling the key down on pressing it, but I want it to release when I physically release the other key (w).

Code: Select all

q::
KeyDown := !KeyDown
If KeyDown
	SendInput {q down}
Else
	SendInput {q up}
Return
Thank you very much if you can help! I think this will be a popular script as many people prefer toggle sprint but quite a few games out there don't offer it.

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Toggle hold a key down until a DIFFERENT key is released.  Topic is solved

Post by mikeyww » 04 May 2021, 08:47

Code: Select all

$q::
Send % "{q " (GetKeyState("q") ? "up" : "down") "}"
SoundBeep, 1000 + 500 * GetKeyState("q")
KeyWait, q
Return

~w Up::
SoundBeep, 800
Send {q up}
Return
As usual, when you press q, w stops.

kain067
Posts: 5
Joined: 04 May 2021, 07:36

Re: Toggle hold a key down until a DIFFERENT key is released.

Post by kain067 » 04 May 2021, 09:43

Thank you; brilliant!

I removed the SoundBeep lines, but thanks as those showed how and when it was working.

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Toggle hold a key down until a DIFFERENT key is released.

Post by mikeyww » 04 May 2021, 12:05

I'm glad it works. Enjoy!

Post Reply

Return to “Gaming Help (v1)”