Public Group

# Need help with how to keep track of what keys have been pressed/released in WIN32.

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

## Recommended Posts

Hi, I am trying to create a buffer of the keys that are being pressed and released in WIN32. Currently I have..
void Input::ReadInput(MSG &msg)
{
switch(msg.message)
{
case WM_KEYDOWN:
// Need to keep track of all the keys that are down
break;
case WM_KEYUP:
// Then need to keep track of when they are released
break;
}
}

This is called once everytime through the game loop. The only way I can really think on how to do this is by maybe using an array of bool type that represent every key possible. But I have ZERO idea how to use the MSG object to check EVERY key. Any suggestions? Regards Chad

##### Share on other sites
Read the documentation for WM_KEYDOWN to see how you can find out which key was pressed.

[Edited by - jpetrie on November 3, 2009 4:14:46 PM]

##### Share on other sites

Also, what are you referencing when you said "October is the new August".

Regards

##### Share on other sites
Unless I'm misunderstanding your problem, you shouldn't need to check every key every time you get a msg.

void Input::ReadInput(MSG &msg, bool keyStates[]){	switch(msg.message)	{		case WM_KEYDOWN: 			keyStates[msg.wParam] = true;			break;		case WM_KEYUP:			keyStates[msg.wParam] = false;			break;	}}

Just make sure the key state array has a lifetime that spans the length of the application (or when the user can use the keyboard), and make sure that your array is big enough to be indexed by any of the virtual key codes (or use a re-sizable vector or whatnot).

[EDIT] - typos galore

##### Share on other sites
Quote:
 Original post by extralongpantsUnless I'm misunderstanding your problem, you shouldn't need to check every key every time you get a msg.*** Source Snippet Removed ***Just make sure the key state array has a lifetime that spans the length of the application (or when the user can use the keyboard), and make sure that your array is big enough to be indexed by any of the virtual key codes (or use a re-sizable vector or whatnot).

Do you mean to make one state true and one false?

##### Share on other sites
Quote:
Quote:
 Original post by extralongpantsUnless I'm misunderstanding your problem, you shouldn't need to check every key every time you get a msg.*** Source Snippet Removed ***Just make sure the key state array has a lifetime that spans the length of the application (or when the user can use the keyboard), and make sure that your array is big enough to be indexed by any of the virtual key codes (or use a re-sizable vector or whatnot).

Do you mean to make one state true and one false?

##### Share on other sites
Quote:

To step in, it's due to a missing double-quote character. The post should read like this:
Quote:
 Original post by jpetrieRead the documentation for WM_KEYDOWN to see how you can find out which key was pressed.

(I've made note of this bug here, but it still plagues all forms of life!)
Quote:
 Also, what are you referencing when you said "October is the new August".

Everything that shows up in the post after "... documentation for" is part of jpetrie's signature, that particular bit is referencing the mutability of real-world schedules [wink]

##### Share on other sites
So i got the virtual keys working just fine. I am now trying to figure out a clean way to process the A-Z 0-9 keys. THis is what I have
void Input::ReadInput(MSG &msg){	switch(msg.message)	{		case WM_KEYDOWN: 			// Need to keep track of all the keys that are down			m_keyState[msg.wParam] = true;			break;		case WM_KEYUP:			// Then need to keep track of when they are released			m_keyState[msg.wParam] = false;			break;			// Need to figure out how to process a-z 0-9	}}bool Input::GetKeyPress(unsigned char Num){	if(m_keyState[Num] == true)		return true;	return false;}
As you can see I am little lost on my next step. I know how the keys are assigned hex values. But what I don't know is why WM_KEYDOWN and WM_KEYUP does not process those. Any suggestions

Regards.

##### Share on other sites
Windows will send WM_KEYDOWN and WM_KEYUP for all the keys, including a-z and 0-9. If you put a breakpoint on your keydown handler, you should hit it when you press one of those keys.

When checking whether a key is pressed or not, you have to pass the virtual key code (e.g. VK_A, VK_0), not the character value it represents.

If you are referring to getting the character data that those keys generate, look into the TranslateMessage function and the WM_CHAR message.

##### Share on other sites
Ok...I did some testing and i am having issues with WM_KEYDOWN. It is not catching all keystrokes. I have run my program through the debugger a few times now.

1st test - Set a break boint inside the WM_KEYDOWN case. This way I can see when it was being entered. It was only being entered on certain keys I was hitting. For example the arrow keys would trigger the break point but none of the character keys (A-Z 0-9) were working. For that matter a lot of the keys were not working (NumPad - F1 Key etc.). I did this test a couple times to make sure the exact same result happened.

2nd test - I moved the break point into the WM_KEYUP case. This trigger every key release that I did on the keyboard.

Any ideas?

Regards

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 12
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633354
• Total Posts
3011494
• ### Who's Online (See full list)

There are no registered users currently online

×