bubyoz
bubyoz

Reputation: 25

Disable/Block a Key with another Key while its pressed and held down

So in this game im moving my charachter with the WASD keys, but if i hold down the A and D key at the same time, the game register that as a forward movement (W key |) so the charachter starts to move forward instead of the strafe actions (Left) \ (right) /.

So i need a code which is prevents the A and D key simultaneous pressing.

CHECK THIS GIF, SO U CAN SEE WHAT I MEAN!

I want A and D override each other (Im not using the W key), because if i hit both A and D at the same time my character moves forward, not like this \ / and i want to avoid the forward movements.

I want insantly changed fast Left \ and Right / strafing only.

Here is the code what i got so far:

~a::
If (GetKeyState("d", "p"))
{
    Send {d up}
    d = 0
}
Return

~d::
If (GetKeyState("a", "p"))
{
    Send {a up}
    a = 0
}
Return

a up::
If (d)
{
    Send {d down}
    d = 0
}
Return

d up::
If (a)
{
    Send {a down}
    a = 0
}
Return

Basicly this code almost working.

The problem is if i don't change the numbers i can't change directions continuously i need to let go the keys. It stops after 1 direction change. If i change the numbers its working, but after a few direction change its getting toggled either left or right. Even if i let it go its moving left or right....

Any ideas? thx

Upvotes: 0

Views: 1150

Answers (2)

GroggyOtter
GroggyOtter

Reputation: 509

This should work. Try it and let me know.

$*a::
$*d::
    SendInput, {a Up}{d Up}
    StringReplace, hk, A_ThisHotkey, % "$*"
    SendInput, {%hk% Down}
    KeyWait, % hk
    Send, {a Up}{d Up}
return

Upvotes: 2

Martin
Martin

Reputation: 604

EDIT: You can play around with the code below. Maybe it will help you out

#SingleInstance, force
#Persistent
#NoEnv
#MaxThreadsPerHotkey, 1

~a & d::
~d & a::
Send, {a up}
key := "d"
SetTimer, pressdown, 10
return

~d::key := "d"
~a::key := "a"
~a up::key := "d"
~d up::key := "a"

pressdown:
if GetKeyState(key, "p")
{
    SendInput, {%key% down}
    SetTimer, pressdown, 30
}
else {
    SetTimer, pressdown, Off
    SendInput, {%key% up}
}
return

This script cannot allow A, or D to be simultaneously pressed.

Upvotes: 0

Related Questions