Trying to use GetKeyboardState

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

Recommended Posts

At the begging of each game loop I fill my array of chars using GetKeyboardState. It's been working fine with other projects and now all of the sudden it seems as though it isn't. I think I'm getting the wrong values for some reason. Letters and numbers work fine, each of their values = their associated ascii values. But characters like - = [ ] \ are no longer ascii values.

[ = 219 when it should be 91 etc

Why would this be happening?

Share on other sites

At the begging of each game loop I fill my array of chars using GetKeyboardState. It's been working fine with other projects and now all of the sudden it seems as though it isn't. I think I'm getting the wrong values for some reason. Letters and numbers work fine, each of their values = their associated ascii values. But characters like - = [ ] \ are no longer ascii values.

[ = 219 when it should be 91 etc

Why would this be happening?

Did you change your keyboard layout? Maybe a program changed it, a restard might help. Or try set your keyboard language manually.

Share on other sites
I haven't changed anything like that, I'll try a restart.

Share on other sites
Restart didn't change anything.

But if my keyboard layout was changed or something, wouldn't I be unable to type the characters with incorrect values?

Share on other sites
How are you using the results of GetKeyboardState?

Share on other sites
char* m_pcKeyState; void Update() { GetKeyboardState((PBYTE)m_pcKeyState); }

I call update every time the loop runs

Share on other sites
How are you determining whether or not a key is down? (sorry, I should have been more clear)

Share on other sites
inline bool Keyboard::IsKeyDown(unsigned int a_uiKey)const { return (m_pcKeyState[a_uiKey] & 0xf000) != 0; }

Share on other sites
I'm not totally sure what's up with your program, but I can see that you are using the wrong mask value, for one. It should be 0x80 (1000 0000 in binary) and not 0xF000 (which is 1111 0000 0000 0000) as you are really only trying to test the most significant bit in the byte (unless you are looking to test the state of a toggle key (shift, caps lock, ctrl, or alt)).

For example, your 91 from above looks like this in binary: 0101 1011 and 219 looks like this: 1101 1011.
Notice that the only difference between the two is that bit on the beginning?

Share on other sites
Ty for pointing out I was using the wrong mask. I changed it but I'm still having the same problem?

Share on other sites
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?

Share on other sites
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

Share on other sites

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?).

Share on other sites
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; } 

Share on other sites

[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

Share on other sites

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]

Share on other sites
Well, best of luck and glad to be of help

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