Toogle "W\A\S\D" on hotkey press.

Ask gaming related questions (AHK v1.1 and older)
n1ce0n3
Posts: 21
Joined: 18 Feb 2018, 04:15

Toogle "W\A\S\D" on hotkey press.

Post by n1ce0n3 » 17 Aug 2022, 14:49

You can ignore the timer part, it is not relevant to the question, just felt like posting full script I have right now just in case.
The idea is to release"W\A\S\D" on hotkey press, do the required combo (since it gets messed up if during the combo any of the keys are pressed) and push them down again once the combo is done.
I was trying to use BlockInput to kinda avoid it, but blockinput doesn't really seem to work well with keywait, however if I don't use keywait - the hotkey goes on a loop.

Code: Select all

#NoEnv
#Persistent
#IfWinActive, ahk_exe required_app.exe
SetCapsLockState, AlwaysOff
ListLines Off
SetBatchLines, -1
SetKeyDelay, 0, 15
SetWinDelay, -1
SetControlDelay, -1
SendMode, Input
MyTimerPrior := -1


fixTimer1:
   MyTimerPrior := -1
   SetTimer, fixTimer1, Delete
return




fixKeys:
   if (GetKeyState("w", "P") && !GetKeyState("w", "L")) {
      SendInput, {Blind}{w down}
   }
   if (GetKeyState("a", "P") && !GetKeyState("a", "L")) {
      SendInput, {Blind}{a down}
   }
   if (GetKeyState("d", "P") && !GetKeyState("d", "L")) {
      SendInput, {Blind}{d down}
   }
   if (GetKeyState("s", "P") && !GetKeyState("s", "L")) {
      SendInput, {Blind}{s down}
   }
return

XButton1:
   SendInput, {Blind}{w up}{s up}{d up}{a up}
   Sleep, 2
   if (MyTimerPrior == 0) {
      SendInput, {Blind}{Space down}
      Sleep, 1
      SendInput, {Blind}{s down}
      Sleep, 1
      SendInput, {Blind}{s up}{Space up}
      Sleep, 1
   }
   Sleep, 2
   SendInput, {Blind}{s down}
   Sleep, 1
   SendInput, {Blind}{s up}
   Sleep, 3
   SendInput, {Blind}{w down}
   Sleep, 1
   SendInput, {Blind}{w up}
   Sleep, 3
   SendInput, {Blind}{RButton}
   MyTimerPrior := 0
   SetTimer, fixTimer1, 700
return

~*$XButton1::
   gosub, XButton1
   KeyWait, XButton1
   gosub, fixKeys
return

~*$w::return
~*$a::return
~*$s::return
~*$d::return


w & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return


a & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return

s & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return

d & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return
Last edited by BoBo on 17 Aug 2022, 15:21, edited 1 time in total.
Reason: Changed "Is there a better solution?" to "Toogle 'W\A\S\D' on hotkey press" to be more specific.

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

Re: Toogle "W\A\S\D" on hotkey press.

Post by mikeyww » 18 Aug 2022, 07:01

Just an idea here: you might want to try a three-line demonstration that you think would achieve this. You can then examine the :arrow: KeyHistory to see what is actually happening. That can then serve as a foundation for adjusting the script.

n1ce0n3
Posts: 21
Joined: 18 Feb 2018, 04:15

Re: Toogle "W\A\S\D" on hotkey press.

Post by n1ce0n3 » 28 Aug 2022, 02:05

mikeyww wrote:
18 Aug 2022, 07:01
Just an idea here: you might want to try a three-line demonstration that you think would achieve this. You can then examine the :arrow: KeyHistory to see what is actually happening. That can then serve as a foundation for adjusting the script.
Well, the script is kind of working as intended 90% of the times the way I wrote it, though, I felt like there has to be an easier solution for that whole thing, that wouldn't take literally 125 lines of code to implement that for the 3 hotkeys.
For me the way I did it - feels like it's more a workaround rather than a proper way of doing what I desire.

Basically, that is all the code I had to write for these 3 hotkeys just to release WASD on initial press and after the hotkey finishes its' work - to press these keys again.


Code: Select all

fixKeys:
   if (GetKeyState("w", "P") && !GetKeyState("w", "L")) {
      SendInput, {Blind}{w down}
   }
   if (GetKeyState("a", "P") && !GetKeyState("a", "L")) {
      SendInput, {Blind}{a down}
   }
   if (GetKeyState("d", "P") && !GetKeyState("d", "L")) {
      SendInput, {Blind}{d down}
   }
   if (GetKeyState("s", "P") && !GetKeyState("s", "L")) {
      SendInput, {Blind}{s down}
   }
return




~*$MButton::
   gosub, MButton
   KeyWait, MButton
   gosub, fixKeys
return

~*$XButton1::
   gosub, XButton1
   KeyWait, XButton1
   gosub, fixKeys
return

~*$XButton2::
   gosub, XButton2
   KeyWait, XButton2
   gosub, fixKeys
return


~*$w::return
~*$a::return
~*$s::return
~*$d::return


w & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return


w & XButton2::
gosub, XButton2
KeyWait, XButton2
gosub, fixKeys
return


w & MButton::
gosub, MButton
KeyWait, MButton
gosub, fixKeys
return


a & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return


a & XButton2::
gosub, XButton2
KeyWait, XButton2
gosub, fixKeys
return


a & MButton::
gosub, MButton
KeyWait, MButton
gosub, fixKeys
return


s & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return


s & XButton2::
gosub, XButton2
KeyWait, XButton2
gosub, fixKeys
return


s & MButton::
gosub, MButton
KeyWait, MButton
gosub, fixKeys
return

d & XButton1::
gosub, XButton1
KeyWait, XButton1
gosub, fixKeys
return


d & XButton2::
gosub, XButton2
KeyWait, XButton2
gosub, fixKeys
return


d & MButton::
gosub, MButton
KeyWait, MButton
gosub, fixKeys
return


Last bumped by n1ce0n3 on 28 Aug 2022, 02:05.

Post Reply

Return to “Gaming Help (v1)”