how to use GetKeyboardState() ?

Started by
4 comments, last by redmonkey 23 years ago
i need to get input for a console side of my game, (as a standalone server for example) but without using cin>> or gets() every loop, so i opted for getkeyboardstate to capture what keys are down (instead of using wm_keydown as im not creating a window) but this code fills newkeys, seemingly randomly, wether or not any keys are down unsigned char newkeys[256]; if (!GetKeyboardState(newkeys)) return false; has anyone got a workin example of this, for storage for the state of every key, or how i should access it if its not just 1(on) and 0(not down) __________________ graham "red" reeves. red@deadpenguin.org www.deadpenguin.org
__________________graham "red" reeves.[email=red@deadpenguin.org]red@deadpenguin.org[/email]www.deadpenguin.org
Advertisement
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);}  


Regards,
Jumpster
Regards,JumpsterSemper Fi
jsut before i try this, what am i expecting btw? 1 for on and 0 for off right?

(thanks for the reply too

__________________
graham "red" reeves.

red@deadpenguin.org
www.deadpenguin.org
__________________graham "red" reeves.[email=red@deadpenguin.org]red@deadpenguin.org[/email]www.deadpenguin.org
no matter, got it thanks a lot, the web seems only to have delphi and vb examples :]

__________________
graham "red" reeves.

red@deadpenguin.org
www.deadpenguin.org
__________________graham "red" reeves.[email=red@deadpenguin.org]red@deadpenguin.org[/email]www.deadpenguin.org
How about the following

#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

And use a switch statement, Not the same though, but looks simpler to me.
Hello from my world
The previous one is what i use, but i dont think its neccessary to use the ?: test in there for it to still work.

Anyway, what jumpster''s code does is shift the high bit all the way to the low bit in a sense, so you can check to see it its on or off. On would be if ((int)key[x] == 1) and off would be if ((int)key[x] == 0).
right, i got it all workin now and for reference this is how im doing it...


int getinput(void)
{
bool olddown;

for (int i=0;i<256;i++)
{
if(!keypress && keydown)<br> olddown=true;<br> else<br> olddown=false; //okay to chnage keypress<br><br><br> keydown[vktochar(i)]=(char) (GetAsyncKeyState(i) >> 8);<br><br> if (!olddown)<br> keypress=keydown;<br> }<br><br> return true;<br>}<br></code><br><br>keypress gets turned off when used (for typing etc) and the GetAsyncKeyState() isnt called 256 times a tick </i> <br><br>__________________<br>graham "red" reeves.<br><br><a href="mailto:red@deadpenguin.org">red@deadpenguin.org</a><br><a href="http://www.deadpenguin.org">www.deadpenguin.org</a>
__________________graham "red" reeves.[email=red@deadpenguin.org]red@deadpenguin.org[/email]www.deadpenguin.org
right, i got it all workin now and for reference this is how im doing it...


int getinput(void)
{
bool olddown;

for (int i=0;i<256;i++)
{
if(!keypress && keydown)<br> olddown=true;<br> else<br> olddown=false; //okay to chnage keypress<br><br><br> keydown[vktochar(i)]=(char) (GetAsyncKeyState(i) >> 8);<br><br> if (!olddown)<br> keypress=keydown;<br> }<br><br> return true;<br>}<br></code><br><br>keypress gets turned off when used (for typing etc) and the GetAsyncKeyState() isnt called 256 times a tick </i> <br><br>__________________<br>graham "red" reeves.<br><br><a href="mailto:red@deadpenguin.org">red@deadpenguin.org</a><br><a href="http://www.deadpenguin.org">www.deadpenguin.org</a>
__________________graham "red" reeves.[email=red@deadpenguin.org]red@deadpenguin.org[/email]www.deadpenguin.org

This topic is closed to new replies.

Advertisement