Sign in to follow this  
Skalli

Several questions concerning GetKeyboardState()

Recommended Posts

Skalli    122
I want to use GetKeyboardState() in order to check the pressed keys on the keyboard once and then analyze them. Like when someone pressed key up for moving forward in a game he can move forward and press another key to jump while still walking forward or pressing right shift to walk slow. But now I have some problems working with GetKeyboardState. The most important problem is that when I press a key and release it the key is still signed as pressed. So if I press key up once the characte would run until it is pressed again. What am I doing wrong that the Key is stated as pressed when it is not? The next question is: There is a high- and a low-order bit. The high-order bit is used to check if the key is currently pressed. The low order bit is used for toogle keys like capslock to state if it is active. But how can I check the low-order bit? And the last question: This function kinda hooks the keyboard. So if I press a key while the window is out of focus the keypress is still captured. What would be the most effective way to only capture keys that are pressed while the window has the focus? Should i use a flag that is toogled when the window gains/looses the focus? Or can I check the focus with a function before I check the keyboard? Thanks for helping. Greetings, Skalli

Share this post


Link to post
Share on other sites
MJP    19753
GetKeyboardState should reflect the status of keyboard based on the messages processed on the calling thread. So..

a)Keys should go back to being marked as "up" as soon as you process the WM_KEYUP message

b)Keys should only been marked when your window has focus, since you only receive WM_KEYDOWN and WM_KEYUP messages when this is the case. It doesn't globally hook the keyboard or anything like that.

I'm not really sure why you're experiencing the behavior you're describing.

Share this post


Link to post
Share on other sites
Skalli    122
Well, I could probably solve the problems easier when I put the function in the WM_KEYDOWN message. But i don't need them there... that would at least solve the problem with the focus. But i used a flag to work around that particular problem (Set true when WM_SETFOCUS is called and false when WM_LOSTFOCUS is called).


if(m_sbHasFocus)
{
Key = GetAsyncKeyState(VK_RCONTROL);
if(Key)
PostQuitMessage(0);
if(!GetKeyboardState(m_lpKeyboardState))
MessageBox(m_oParent, "Couldn't get keyboard state","Error: ProgressKeyboardInput()", 0);
if(m_lpKeyboardState[VK_LCONTROL])
MessageBox(m_oParent, "Left control down","Error: ProgressKeyboardInput()", 0);
}


That's my source code.
Key is from type int. However, as well GetAsyncKeyState as well as GetKeyboardState currently hook globaly and don't get the key up event. It's totally confusing, so if anyone has an idea. That would really help me to go on...

What helps is when I manually set the pressed key to handled...
The changed code part looks like this:

if(m_lpKeyboardState[VK_LCONTROL])
{
MessageBox(m_oParent, "Left control down","Error: ProgressKeyboardInput()", 0);
m_lpKeyboardState[VK_LCONTROL] = 0;
if(!SetKeyboardState(m_lpKeyboardState))
MessageBox(m_oParent, "Couldn't set keyboard state","Error: ProgressKeyboardInput()", 0);
}


That solution isn't entirely helpfull. Why?
If I check for another key like VK_ESCAPE in the event loop:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SETFOCUS:
Engine::m_sbHasFocus = true;
break;
case WM_KILLFOCUS:
Engine::m_sbHasFocus = false;
break;
case WM_SIZE:
Engine::m_sbResize = true;
break;
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0); // doesn't work anymore... ^^
break;
}
}

break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return DefWindowProc( hWnd, message, wParam, lParam );
}

the escape key press isn't handled anymore...

And also i don't like to use code I obviously don't understand completely.

Greetings,
Skalli

P.S.: After I got some sleep I'll try to put the GetKeyboardState in the WM_KEYDOWN event and send the keyboard array to the progressing method of my class... We'll see how that will work out...

Share this post


Link to post
Share on other sites
solacedagony    123
Quote:
P.S.: After I got some sleep I'll try to put the GetKeyboardState in the WM_KEYDOWN event and send the keyboard array to the progressing method of my class... We'll see how that will work out...


Take a look at NeHe's basecode. He uses WM_KEYUP and WM_KEYDOWN to setup an array of keys and it works well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this