Skip to main content
GameDev.net gamedev.net
🔒 Locked

WM_KEYDOWN vs. GetAsyncKeyState

Started by Bad Ham May 12, 2007 at 10:30 PM 4 replies 4.2k views
Original Post
Bad Ham
Bad Ham
Hi everyone. My input library is giving me no end of headaches, currently using WM_KEYDOWN/KEYUP. Mostly these are related to issues of key repetition. Ie. If I press a button once, it registers as pressed through 5-10 game loops or more, but if I use a function to limit the number of presses, of course then I can't (for instance) use a held-down key to move forward. I'm thinking that using GetAsyncKeyState instead of listening for WM_ messages and updating an array might fix this...but is GetAsyncKeyState particularily slower? Or is there some other way I should be doing this? What I'd like is to have a key only register as "pressed" for one frame if it was just pressed, but register as "pressed" for many frames if the user is indeed holding down the key.
ßαδ Hαm
Merick Zero
Merick Zero
I don't really know the specifics for doing this in c (or any language except basic or the psp lua player), but at the end of the main loop you could set a variable old_input, then when the program loops back to where you are checking for keyboard input you could use old_input to check whether or not a certain key was pressed down during the previous program loop
glBender
glBender
Regardless of how you end up getting the input, the message loop or GetAsyncKeyState, in your library you should differentiate between a keypress and keydown. Have one function for keypress, that only returns true once for each time the key is pressed down, and then a function for keydown, return true the entire time the key is down.
Bad Ham
Bad Ham
That would work, separating them, I wasn't under the impression that was done often.

But just in terms of implementation, is there any advantage to WM_KEY* messages over GetAsyncKeyState or vice-versa?
ßαδ Hαm
glBender
glBender
I can't think of any distinct advantages... I think you should go with whichever you like. Of course, I'm sure there is someone here that could tell you advantages / disadvantages for them. But I think whichever makes more sense to you is the one you should go with.
RobN
RobN
You could also look at SetWindiowsHookEx() and specify a keyboard hook(global or local).

GetAsyncKeyState() is going to be global so it will be triggerd even if your apps minimized.
WM_Keydown will be relative your window.

WM_KEYDOWN i expect will be faster than GetAsnycKeystate as the message is being sent anyway your just not testing for it.

I would go with wm_keydown.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.