[win32] - can i get the keyboard keys flag?

i can get the window style flag and then i can see what styles was added. can i do the same fo the keyboard?

think in these way: i have the key 'a' and 'b'... now how can get that flag?(but think that you don't know what keys was pressed)
? What flags are you referring to? Are you asking about the key state (pressed or released)?
yes... 1 value that can tell me about all keys if they are pressed\released.
Last edited on
There are over 100 keys on a keyboard.

The state of all 100+ keys cannot possibly fit in a single value.

If you want the state of one key, you can use GetAsyncKeyState.
If you want the state of all keys, you can use GetKeyboardState

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646299%28v=vs.85%29.aspx

Though the comments on that page make it sound like GetKeyboardState is flakey.
you have right.. the MSDN can be very confused sometimes.
but i found these topic in diferent forum, with your help:
http://www.gamedev.net/topic/43463-how-to-use-getkeyboardstate-/
i can use a for loop for test all keys and put them in a variable ;)
thanks for all
i'm testing these code without sucess :(
1
2
3
4
char keysstate(256);
    GetKeyboardState((PBYTE)keysstate);
    string i=to_string(keysstate[65]).c_str();
    MessageBox(a,i.c_str(),"keystate",MB_OK);

the 3rd line give me an error: "invalid types 'char[int]' for array subscript"
what i'm doing wrong?
string i=to_string(keysstate[65]).c_str();

- If to_string returns a string... why are you calling c_str here? Why not just assign the string to i directly?

- keysstate[x] is likely going to be 0 (a null character) or 0x80 (a weird non-ascii character), so converting it to a string and printing it won't really be useful.

- How is putting the keysstate array into a string helpful, anyway? A string is just a different kind of array... so why not just use the keysstate array directly? Why convert to string?
Last edited on
i'm testing the code... is for show a integer value in message box
This quick-n-dirty code works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    BYTE keys[256];

    for(int i = 0; i < 5; ++i)
    {
        Sleep(1000);

        GetKeyboardState(keys);
        string s = to_string( (int)keys['A'] );
        MessageBoxA(NULL,s.c_str(),"key state",MB_OK);
    }
}
finally i put these code to work ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
oid GetKeyBoardKeyState(int *keys)
{
    for(int i=0; i<256; i++)
    {
        if(GetAsyncKeyState(i)>>8)
        {
            keys[i]=1;
        }
        else
        {
            keys[i]=0;
        }
    }
}

//in main

 int kbstate[256]={0};
        GetKeyBoardKeyState(kbstate);
        MessageBox(a,to_string((int)kbstate[65]).c_str(),"hi",MB_OK);

thanks for all.. realy Disch... thanks for all
Last edited on
Well... again... with GetAsyncKeyState... you are only interested in the high bit:

1
2
3
if(GetAsyncKeyState(i)) // <- wrong

if(GetAsyncKeyState(i) & 0x8000) // <- right 


And this code isn't using GetKeyboardState at all. Was that intentional?
thanks for correct me that.. thanks.
"And this code isn't using GetKeyboardState at all. Was that intentional?"
sorry about that.. i'm confused of what means 'intentional'. but i use these new function, because i wasn't get sucess with GetKeyboardState () and more: now i can test the no key and any key ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void GetKeyBoardKeyState(int *keys)
{
    bool NoKey=true;
    bool AnyKey =false;
    for(int i=0; i<256; i++)
    {
        if(GetAsyncKeyState(i) & 0x8000)
        {
            keys[i]=1;
            NoKey=false;
            AnyKey=true;
        }
        else
        {
            keys[i]=0;
        }
    }
    if(NoKey==false)
    {
        keys[256]=0;
    }
    else
    {
        keys[256]=1;
    }

    if(AnyKey==true)
    {
        keys[257]=1;
    }
    else
    {
        keys[257]=0;
    }
}

const int AnyKey=257;
const int NoKey=256;

thanks for all.. realy thanks for all
heres another function for get the flag of what is pressed in same time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int GetAllKeyPressed()
{
    int key=0;
    bool FirstKey=false;

    for(int i=0; i<256; i++)
    {
        if(GetAsyncKeyState(i) & 0x8000)
        {
            if(FirstKey==false)
            {
                key=i;
                FirstKey=true;
            }
            else
            {
                key=key & i;
            }
        }
    }
    return key;
}

//heres how uese it:
//if the actual pressed keys are the  left key and 'a' key,  then do something
if(GetAllKeyPressed()==VK_LEFT & 'A')
    //do something 

i hope these code helps more people
if(GetAllKeyPressed()==VK_LEFT & 'A')

This would not work as you expect
Topic archived. No new replies allowed.