0
votes

I'm interested in disabling the w,a,s, & d keys while holding down the left mouse button. (Games such as cs:go and valorant penalize shooting while moving, so I'd like to preclude that situation entirely). I just learned about autohotkey, so I'm sorry if I offend someone with this attempt at code(which obviously doesn't work):

while (GetKeyState("LButton", "P"))
{
    w::return
    a::return
    s::return
    d::return       
} 

Thanks, much appreciated!

3

3 Answers

1
votes

I just want to add in the solution that @Spyre hinted at. The script does indeed become much smaller when you use the #If directive, and when the if statement fails, the hotkey doesn't fire, so it doesn't add to your overall hotkeys per interval. Here is my version of the script:

#If GetKeyState("LButton", "P")
w::
a::
s::
d::
Return
1
votes

Use #If (docs):

#If, GetKeyState("LButton")
w::
a::
s::
d::return  
#If

And if #If were to cause you trouble (like warned about in the documentation) (it's not going to happen if your script is this small and simple), you'd want to do turn on and off the hotkeys with the Hotkey command.
For example:

~LButton::ConsumeASDW("On")     ;press down
LButton Up::ConsumeASDW("Off")  ;release

ConsumeASDW(OnOff)
{
    for each, key in StrSplit("asdw")
        Hotkey, % key, Consumer, % OnOff
}

Consumer()
{
    Return
}
0
votes

You've got the right idea, but what you currently have will be auto-executed at the beginning of the script, and after one run through of checking whether or not the LMB is held down, the script will stop doing anything.

My approach, using the $ modifier with hotkeys to make sure the hotkey doesn't recursively trigger itself.

$w::
if (GetKeyState("LButton", "P"))
    return
Send w
return
$a::
if (GetKeyState("LButton", "P"))
    return
Send a
return
$s::
if (GetKeyState("LButton", "P"))
    return
Send s
return
$d::
if (GetKeyState("LButton", "P"))
    return
Send d
return

There might be some more efficient way of declaring the condition of the hotkeys using the #If directive, but this should the job that you need it to.