Key press issue

Started by
3 comments, last by Shadekf 14 years, 11 months ago
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.
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.
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
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.
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.

This topic is closed to new replies.

Advertisement