Key press issue
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement