Newbie question: WASD keys, need a bit of help and pointers

Ask gaming related questions (AHK v1.1 and older)
User avatar
Shadrach
Posts: 18
Joined: 18 Nov 2017, 08:02

Newbie question: WASD keys, need a bit of help and pointers

18 Nov 2017, 08:19

Hey,
pretty much a total newbie to AHK - I know basic programming and had a look at the documentation, and although a bit obtuse, I think I can manage.
However, my first project is a basic WASD key script for a game that uses only the arrow-keys for movement. Should be simple enough, but I've run into some issues and not sure if I've solved them the best way.

I am doing a simple map of WASD to the arrow keys. First I started with a simple Send, {Down} but apparently it didn't take, there seemed to be movement but it seemed to be slow, like only one keypress. I tested in another application (notepad) and pressing S did send downarrow commands while being held down. If I changed to Send,{Up down} it just held the key down (makes sense though).

So what finally worked was this:

Code: Select all

#IfWinActive The Operational Art of War
w::
   Send,{Up down}
   KeyWait, w
   Send,{Up up}
Return
a::
   Send,{Left down}
   KeyWait, a
   Send,{Left up}
Return
s::
   Send,{Down down}
   KeyWait, s
   Send,{Down up}
Return
d::
   Send,{Right down}
   KeyWait, d
   Send,{Right up}
Return
#IfWinActive
It works, but not sure if that's the recommended way to do it? I've found several examples but they seem even more complicated than what I'm trying to achieve.

I think (a theory) is that the game has some sort of keyboard code that interferes with AHK in some way.

Generally it's a bit weird, for instance after I remapped 'A' I thought I needed to remap the original A to Ctrl+A and then send A. But for some reason I didn't need to, Ctrl+A did what A originally did, some sort of 'fallback', I dunno.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Newbie question: WASD keys, need a bit of help and pointers

18 Nov 2017, 09:24

You're better off to use remappings: w::Up, etc..
User avatar
Shadrach
Posts: 18
Joined: 18 Nov 2017, 08:02

Re: Newbie question: WASD keys, need a bit of help and pointers

18 Nov 2017, 09:52

Thanks a bunch for the reply - just wondering, what's the difference between remapping and using the Send command?

I've seen examples of people using both.

Will try it now.
Last edited by Shadrach on 18 Nov 2017, 10:28, edited 2 times in total.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Newbie question: WASD keys, need a bit of help and pointers

18 Nov 2017, 10:20

A remapping is a AHK built-in hotkey combination, which also uses Send:

Code: Select all

;a::b produces

*a::
SetKeyDelay -1   ; If the destination key is a mouse button, SetMouseDelay is used instead.
Send {Blind}{b DownTemp}  ; DownTemp is like Down except that other Send commands in the script won't assume "b" should stay down during their Send.
return

*a up::
SetKeyDelay -1  ; See note below for why press-duration is not specified with either of these SetKeyDelays.
Send {Blind}{b Up}
return
Main difference with a 'plain' Send hotkey is that a remapping doesn't interfere with physically pressed modifier keys (Ctrl, Alt, ...) due to the * in the trigger hotkey and {Blind} in the output; and keeping a key pressed keeps up- and down events aligned, e.g; dddddddddddddu instead of dudududududududu, by defining an up and down hotkey.

If you want a key to behave like another key; use a remapping (or recreate the code above for each key :think: ).
User avatar
Shadrach
Posts: 18
Joined: 18 Nov 2017, 08:02

Re: Newbie question: WASD keys, need a bit of help and pointers

18 Nov 2017, 10:28

So I just tried it, and it works very well, thanks a lot! Especially helpful is the explanation of how the code actually is called in the background for the built-in.

However I'm still confused on the difference since both methods seem to work.

During testing, just using Send worked fine in for instance notepad and sent a stream of keys, while in-game it appears Send just sent a single key?

Also with the remapping method, I actually had to rebind the keys it replaced, like +a::+a which meant it didn't register Shift+A as a different key than a.

I guess a remapping actually *replaces* the keys on your keyboard while the Send command just sends them to the window. But why wouldn't it send a bunch of {Down} commands when S was pressed when I just used Send {Down}?

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 41 guests