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

Jul 16, 2014 at 7:28pm
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)
Jul 16, 2014 at 11:37pm
? What flags are you referring to? Are you asking about the key state (pressed or released)?
Jul 17, 2014 at 9:51am
yes... 1 value that can tell me about all keys if they are pressed\released.
Last edited on Jul 17, 2014 at 9:54am
Jul 17, 2014 at 10:07am
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.
Jul 17, 2014 at 10:21am
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
Jul 17, 2014 at 10:34am
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?
Jul 18, 2014 at 5:06pm
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 Jul 18, 2014 at 5:07pm
Jul 18, 2014 at 6:28pm
i'm testing the code... is for show a integer value in message box
Jul 18, 2014 at 6:58pm
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);
    }
}
Jul 18, 2014 at 7:07pm
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 Jul 18, 2014 at 7:41pm
Jul 18, 2014 at 7:41pm
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?
Jul 18, 2014 at 8:26pm
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
Jul 20, 2014 at 9:47pm
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
Jul 21, 2014 at 12:16am
if(GetAllKeyPressed()==VK_LEFT & 'A')

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