Quote:Original post by skulldrudgery
Doh, I have to make a PBYTE myself! Ok this compiles fine, but how am I supposed to use it to get the keypress. It doesn't seem to work the way I'm doing it.
*** Source Snippet Removed ***
EDIT: Maybe I'm getting in a little over my head with this GetKeyboardState() setup. I just want to get some keyboard input. Maybe some advice on one of the other methods I mentioned?
A PBYTE is just a pointer, which is a nice convenient thing for passing to functions, but it has to point at memory that belongs to you before you can do anything useful with it.
The simplest way in this case is to have an actual array allocation:
char keyData[256];// An array name can be used as a pointer to the beginning of the array:GetKeyboardState(keyData);// Now *examine* the data in keyData:if (keyData[VK_SPACE] & 128) { // space was hit.}
If you are using the standard library containers, you should be aware that the storage space of std::vector is guaranteed to be a single chunk, which you can use as an array:
std::vector<char> keyData(256);// This isn't an array, but we can use its overloaded operator[] to access the// first element, and then get its address, which is a pointer:GetKeyBoardState(&keyData[0]);// etc.
Mixing this API with getch() is simply not a good idea - each will make its own attempt to read the keyboard, and something is bound to get messed up. They certainly won't make use of the same reading. Also, getch() works fundamentally on a higher level; it is not really checking key states, but instead waiting for the next character to arrive - after the OS has done all the work of translating keypress durations into "character input" events. It's really more like working with cin, except that it isn't line-buffered.
Like I said:
Quote:
This tells how to interpret the contents of the buffer after making the GetKeyboardState() call. You index the array with the virtual key code corresponding to the key you care about, yielding an element of the array of bytes, which therefore is a byte. Then you test its high-order bit (bitwise AND with 128) to see if the key is down.