Trying to use GetKeyboardState

Started by
15 comments, last by s73obrien 12 years, 5 months ago
Maybe I misunderstood the problem? O.o

You are saying that if, for example, you wanted to test to see if the a key is down, you would access it like this:

if ((key_array[key_id] & 0x80) != 0)
key_down = true;
else
key_down = false;

but you are finding that sometimes it is false when you expect it to be true and vice-versa?
Advertisement
For all letters and numbers, when pressed down the appropriate ascii value in char* m_pcKeystate (when tested for being pressed down) returns true.
For other values, such as for keys -=[]\ etc the wrong ascii value keys return true for being pressed down.

For example...

for (int a = 0; a < 256; a++)
{
if (m_poKeyboard->IsKeyDown(a))
{
//Do stuff
}
}


Inside that loop I test what key is down. If I press W "stuff is done" when a = 87 (the proper ascii value for w).
But when I press [ "stuff is done" when a = 219, when it should be done when a = 91

It's been working fine with other projects and now all of the sudden it seems as though it isn't.
...
Why would this be happening?



char* m_pcKeyState;

void Update()
{
GetKeyboardState((PBYTE)m_pcKeyState);
}



Unless m_pcKeyState points to some previously allocated memory, there is no guarantee that anything will work properly. Since GetKeyboardState expects an array of 256 elements, why not have "char mKeyStates[256]"?

Also, consider dropping the systems hungarian notation; it doesn't provide any benefits, and hurts readability (any guess as to what m_pcacszfitTemp might be used for?).
I think I see what's up with this now. Try using the virtual key codes to index into the array instead of just a number. The extended keys on the keyboard (-=[] etc) are mapped to different numbers than what you'd expect.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx <--- virtual key codes here

To test for the left brace, it seems you must use VK_OEM_4. Not sure how well this will fit into your program.

Also, might I recommend using GetAsyncKeyState? That way your IsKeyDown member of Keyboard would look like this:


bool Keyboard::IsKeyDown(int key_code)
{
return GetAsyncKeyState(key_code) & 0x80;
}

[quote name='reaperrar' timestamp='1320538716' post='4880912']
It's been working fine with other projects and now all of the sudden it seems as though it isn't.
...
Why would this be happening?



char* m_pcKeyState;

void Update()
{
GetKeyboardState((PBYTE)m_pcKeyState);
}



Unless m_pcKeyState points to some previously allocated memory, there is no guarantee that anything will work properly. Since GetKeyboardState expects an array of 256 elements, why not have "char mKeyStates[256]"?

Also, consider dropping the systems hungarian notation; it doesn't provide any benefits, and hurts readability (any guess as to what m_pcacszfitTemp might be used for?).
[/quote]

Fastcall, wouldn't how he has it throw a segfault anyway? I guess I assumed that he was already allocating space for it biggrin.gif

Also, consider dropping the systems hungarian notation; it doesn't provide any benefits, and hurts readability (any guess as to what m_pcacszfitTemp might be used for?).


It's a mandatory requirement of my course to use hungarion notation ><

s73obrien...
Thx, I see what I was doing wrong. Guess it was just lucky that the letters/numbers were mapped to ascii in the virtual key codes, that'd be why I got confused. I've used GetAsyncKeyState [font="Arial"]before but I found problems with it. If I was pressing say, 4 keys at once... only 3 would register.

It's going to be awkward now trying to create strings out of what keyboard input has been pressed :(
[/font]
Well, best of luck and glad to be of help :)

Maybe a switch statement for sorting out which character goes with which keypress?

This topic is closed to new replies.

Advertisement