Jump to content
  • Advertisement
Sign in to follow this  
Shadekf

Key press issue

This topic is 3411 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

Hey guys, Quick question. For detecting keypess's at the moment I'm using: #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) And then in my game loop when I want to check if a key is pressed I use something like: if(KEY_DOWN(VK_ESCAPE)) PostMessage(hWnd, WM_DESTROY, 0, 0); My question is: Is there a function I can call that will return what key was pressed? I'm making a strategy game and different keys will do different things depending on what is selected, so I want to get what key was pressed and then give it to my object to deal with rather than handling everything in the main loop. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Don't cross-post, there's no point.

In answer to your question, the best strategy would be to handle the WM_KEYDOWN and WM_KEYUP events in your message loop.

Share this post


Link to post
Share on other sites
Quote:
My question is: Is there a function I can call that will return what key was pressed?


You should implement something like this in your event loop.
At the time you filled your WNDCLASS/WNDCLASSEX structure you passed a pointer to a window procedure, which was something like
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Listen to the WM_KEYDOWN or WM_INPUT messages to get the desired behaviour, you'll get the key the user has pressed.
WM_KEYDOWN
WM_INPUT

Share this post


Link to post
Share on other sites
You could write a thin layer to translate key presses into actions in your game. That way you could also load the key mapping from disk, so the user can modify it.

Instead of PostMessage(...) you should probably use PostQuitMessage(). The difference is that PostQuitMessage will wait until the message queue is empty before generating a WM_QUIT message. There's more detail here if you're interested.

Hope that helps.

Share this post


Link to post
Share on other sites
My apologies I didn't intentionally cross post. It was lagging and didn't seem to be working so I closed the window and posted it again.

I did check to see if it worked but couldn't see it.

Thanks for the help guys I'll look into these methods. I think fcoelho said what I was after.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!