Block wasd keys unless X key pressed recently Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Erised

Block wasd keys unless X key pressed recently

23 Nov 2017, 09:46

Hey All,

I am a rookie to AHK but am loving it nonetheless.

Currently I'm trying to block the WASD keys during gaming unless I have pressed x within the last 500MS. Currently I am using the quote below
#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.
#Persistent

w::
{
if (A_TickCount - tc <500)
{
sendinput {w down}
sleep 40
sendinput {w up}
}
KeyWait, X , D
tc:=A_TickCount
return
}

a::
{
if (A_TickCount - tc <500)
{
sendinput {a down}
sleep 40
sendinput {a up}
}
KeyWait, X , D
tc:=A_TickCount
return
}

s::
{
if (A_TickCount - tc <500)
{
sendinput {s down}
sleep 40
sendinput {s up}
}
KeyWait, X , D
tc:=A_TickCount
return
}

d::
{
if (A_TickCount - tc <500)
{
sendinput {d down}
sleep 40
sendinput {d up}
}
KeyWait, X , D
tc:=A_TickCount

return
}
The above works but it is clunky, cumbersome and in general not responsive enough for gaming. I have a feeling there is a much simpler way to do this but after spending the last day pouring through AHK forum posts and tutorials I am stumped. Any assistance that could be provided would be wonderful.

Thanks!
James
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Block wasd keys unless X key pressed recently

23 Nov 2017, 17:10

Here's a simpler version that seems to work:

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Event
SetKeyDelay, 0, 40
#Persistent

global tc := A_TickCount

$w::BlockKey("w")

$a::BlockKey("a")

$s::BlockKey("s")

$d::BlockKey("d")

~x::tc := A_TickCount

BlockKey(key)
{
  if (A_TickCount - tc <500)
    send, %key%
}

Some notes:
  • The dollar signs tell the WASD hotkeys not to call themselves, which is what you're doing by having, for example, the 'w' hotkey send 'w'.
  • The tilde sign tells the 'x' hotkey to still send 'x'. Without it, pressing 'x' would only process the assignment, rather than process it and send 'x'.
  • Rather than duplicate the same few lines 4 times, I moved them to a function(BlockKey), which makes things look cleaner. You can always do away with it and add BlockKey's lines back to the hotkeys, if you prefer.
  • I switched you to SendEvent mode, since I, personally, find it more reliable in games and it also allows setting your key delay once at the top of the script, rather than having to add sleeps, like you were doing.
  • If you want to go back to SendInput mode, though, change it via the SendMode line at the top. You don't need to change your Send commands back, since the whole point of SendMode is that it applies to all Send commands.
Last edited by Osprey on 23 Nov 2017, 18:15, edited 2 times in total.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Block wasd keys unless X key pressed recently

23 Nov 2017, 18:06

Another example:

Code: Select all

#NoEnv
#UseHook
SetKeyDelay, 0, 40
time := A_TickCount

#If (A_TickCount - time > 500)
w::
a::
s::
d::return
#If

w::
a::
s::
d::Send % A_ThisHotkey

~x::time := A_TickCount
If you just want to block the keys:

Code: Select all

#NoEnv
time := A_TickCount

#If (A_TickCount - time > 500)
w::
a::
s::
d::return
#If

~x::time := A_TickCount
Erised

Re: Block wasd keys unless X key pressed recently  Topic is solved

24 Nov 2017, 10:29

Thank you both very much. All your solutions worked but the one below worked almost flawlessly in game with no choppiness:

Code: Select all

#NoEnv
time := A_TickCount

#If (A_TickCount - time > 500)
w::
a::
s::
d::return
#If

~x::time := A_TickCount

Thank you again. This is a great community and you two are a perfect example of why!

Cheers
James

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3 and 278 guests