Button held while hotkey is toggled will stick

Ask gaming related questions (AHK v1.1 and older)
sienihemmo
Posts: 2
Joined: 18 Jan 2022, 22:32

Button held while hotkey is toggled will stick

Post by sienihemmo » 18 Jan 2022, 22:53

Hi,

I'm trying to make a script for a friend for War Thunder. He wants the reversing controls to be inverted, since he's used to it from World of Tanks. What I'm trying to do is whenever S is held, A and D are swapped. But the issue is that if S is released before A or D, it will keep sending the inverted input until the physical button is pressed.

Example:
1. I press S and A to reverse and turn, the hotkey correctly sends D instead of A.
2. I release S before releasing A.
3. The script keeps sending D, until the physical button for D is pressed.

What should happen is that the directions swap back as seamlessly as possible. Meaning if I'm holding down A, releasing S should just swap the direction. An acceptable alternative could be for the script to force the release of both A and D when S is released just to stop the issue happening. But I cant for the life of me figure out how to do either. I've tried all manner of SendInput and while loops etc.

Here's what I have currently:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallKeybdHook

#If GetKeyState("s","p") && WinActive("ahk_exe aces.exe")
	$a::d
	$d::a
#If

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Button held while hotkey is toggled will stick

Post by Rohwedder » 19 Jan 2022, 04:11

Hallo,
try:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallKeybdHook

#If WinActive("ahk_exe aces.exe")
*s::
Send,% GetKeyState("a", "p") And GetKeyState("a")?"{Blind}{a Up}{d Down}":
Send,% GetKeyState("d", "p") And GetKeyState("d")?"{Blind}{d Up}{a Down}":
Return
*s Up::
Send,% GetKeyState("a", "p") And !GetKeyState("a")?"{Blind}{d Up}{a Down}":
Send,% GetKeyState("d", "p") And !GetKeyState("d")?"{Blind}{a Up}{d Down}":
Return
#If GetKeyState("s","p") && WinActive("ahk_exe aces.exe")
	a::d
	d::a
#If

sienihemmo
Posts: 2
Joined: 18 Jan 2022, 22:32

Re: Button held while hotkey is toggled will stick

Post by sienihemmo » 19 Jan 2022, 11:50

That ended up removing the functionality from the S key, but I fixed that by adding an extra binding ingame and adding send commands to the code, so that's okay.

The "sticking" behaviour is still present though, it's just reversed now. To reuse my original example, releasing S before A will instead lead to A being sent until it's pressed again. Not as disruptive as the opposite, but I can imagine it could still mess up games since being stationary is important for aiming as well as angling armor.

Here's the tweaked code:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallKeybdHook

#If WinActive("ahk_exe aces.exe")
*s::
Send,{Blind}{Down Down}:
Send,% GetKeyState("a", "p") And GetKeyState("a")?"{Blind}{a Up}{d Down}":
Send,% GetKeyState("d", "p") And GetKeyState("d")?"{Blind}{d Up}{a Down}":
Return
*s Up::
Send,{Blind}{Down Up}:
Send,% GetKeyState("a", "p") And !GetKeyState("a")?"{Blind}{d Up}{a Down}":
Send,% GetKeyState("d", "p") And !GetKeyState("d")?"{Blind}{a Up}{d Down}":
Return
#If GetKeyState("s","p") && WinActive("ahk_exe aces.exe")
	a::d
	d::a
#If

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Button held while hotkey is toggled will stick

Post by Rohwedder » 20 Jan 2022, 13:35

Then,
perhaps:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#InstallKeybdHook

*a::
*d::
SetTimer, *d, 50
K := ["a Up}{d","d Up}{a"][1+(GetKeyState("s","P")^GetKeyState("a","P"))]
Send, {Blind}{%K% Down}
Return
*a Up::
*d Up::
SetTimer, *d, Off
Send, {Blind}{a Up}{d Up}
Return

Post Reply

Return to “Gaming Help (v1)”