Trying to use GetKeyboardState

Started by
15 comments, last by s73obrien 12 years, 5 months ago
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?
Advertisement

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.
I haven't changed anything like that, I'll try a restart.
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?
How are you using the results of GetKeyboardState?
char* m_pcKeyState;

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


I call update every time the loop runs
How are you determining whether or not a key is down? (sorry, I should have been more clear)
inline bool Keyboard::IsKeyDown(unsigned int a_uiKey)const
{
return (m_pcKeyState[a_uiKey] & 0xf000) != 0;
}
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?
Ty for pointing out I was using the wrong mask. I changed it but I'm still having the same problem? :(

This topic is closed to new replies.

Advertisement