Need help with toggling and untoggling Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Denizen 38
Posts: 6
Joined: 16 May 2022, 16:43

Need help with toggling and untoggling

Post by Denizen 38 » 16 May 2022, 17:03

Hi all, I'm looking for some help to troubleshoot a short script that is not quite behaving as expected. For context, the aim of this script is to let me toggle the "Walk" function in-game (which requires holding a key down) by simply clicking it once to toggle on, and clicking it again to toggle off. This bit is simple and works well. However, to complicate matters, I'd also like certain other keys to instantly toggle off the "Walk" function so that if any of them are pressed, it will immediately send a "Key Up" input to the walk modifier.

Here's the code (I will explain the issue underneath):

Code: Select all

	W::
		KeyDown := !KeyDown
		If KeyDown
			{
			SendInput {W down}
			ToolTip, Walk Mode, 85, 1312
			}
		Else
			{
			SendInput {W up}
			ToolTip
			}
	Return
	~Esc::
		SendInput {W up}
		ToolTip
	Return
	~E::
		SendInput {W up}
		ToolTip
	Return
	~G::
		SendInput {W up}
		ToolTip
	Return
The first bit works just fine. I press "W" once and it toggles "Walk mode" on; I press it again to turn it off. And the other three keys also work fine as far as sending the {W up} signal goes. However, I've noticed that whenever the input for {W up} comes from any of the other keys (i.e. Esc, E, and G), it takes two presses – not one – of the "W" key for it to be toggled on again. To clarify:

W (toggle on) >> W (toggle off) >> W (toggle on) = working as intended
W (toggle on) >> Esc or E or G (toggle off) = also working as intended
W (toggle on >> Esc or E or G (toggle off) >> W (toggle on) = does not produce expected result, requires one extra input of "W" to toggle back on

Realistically, this is not a massive issue but I'd like to also treat this as an opportunity to better understand AHK, so any help in figuring this out would be much appreciated.


[Mod action: Moved topic to "Gaming"]

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

Re: Need help with toggling and untoggling  Topic is solved

Post by mikeyww » 16 May 2022, 18:27

Code: Select all

$w::
If !GetKeyState("w") {
 Send {w down}
 ToolTip, Walk Mode, 85, 1312
 SoundBeep, 1500
 Return
}
~Esc::
~e::
~g::
Send {w up}
ToolTip
SoundBeep, 1000
Return

Denizen 38
Posts: 6
Joined: 16 May 2022, 16:43

Re: Need help with toggling and untoggling

Post by Denizen 38 » 16 May 2022, 18:38

Many thanks, Mikey!

Can confirm this fixed the issue perfectly.

Post Reply

Return to “Gaming Help (v1)”