Jump to content
  • Advertisement
Sign in to follow this  
Bad Ham

WM_KEYDOWN vs. GetAsyncKeyState

This topic is 4229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!