tags:

views:

281

answers:

1

I don't know how to write a good question here, but, basically, does anyone know where I can possibly find some C++ source code using these to actually set keyboard state? For some reason using it the way MSDN does on Windows 7 doesn't do...anything at all.

Basic code:

PBYTE keyState;

GetKeyboardState(keyState);

...

// Later on when I need to set the keyboard state (key pressed etc) back to original:
SetKeyboardState(keyState);

and ... nothing happens :(

+1  A: 

From: http://www.gamedev.net/community/forums/topic.asp?topic_id=43463

First off, GetKeyboardState() would be the wrong function to use because as Windows has a chance to process keyboard messages (whether you want it too or not) it updates the results of the keyboard's state for the next call to GetKeyboardState().

Here's a little function that I use to get the status of the keyboard's keys. Be carefull though, depending on how fast your main loop is, it may cause problems if you aren't expecting it.

You need to keep track of whether or not a specific key was pressed the last time you called the ReadKeyboard() function. If your loop polls the keyboard 30 times a second, then pressing a key once probably causes the key to be flagged 3 or 4 calls in a row. Rather confusing sometimes. Just thought I'd mention it.

void ReadKeyboard( char* keys )
{ 
  for (int x = 0; x < 256; x++)
    keys[x] = (char) (GetAsyncKeyState(x) >> 8);
}
Soppus