GetKeyboardState() & SetKeyboardState()

Hi,
I know how to use the GetAsyncKeyState() and GetKeyState() functions;
But I'm having some problems understanding the GetKeyboardState() and SetKeyboardState() functions. I'm trying to understand the GetKeyboardState() first; What exactly does it retrieves? MSDN says "status of the 256 virtual keys"
based on what I got form MSDN, I did the following code.

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
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    COORD cp;
    BYTE k[256];
    system("MODE CON: COLS=49 LINES=17");

    while(1)
    {
        GetKeyboardState(k);
        for(unsigned char y=0; y<16; y++)
        {
            for(unsigned char x=0; x<16; x++)
            {
                if( k[(y*16)+x] )SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
                else             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);

                cout<<(int)k[(y*16)+x]<<"  ";
            }
            cout<<endl;
        }
        Sleep(50);
        cp.X=0;
        cp.Y=0;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cp);
    }
}


The array k[256] gets it's values form the function GetKeyboardState() and display them all on the screen form location k[0] to k[255] in the form of a 16*16 array and the non-zero values are displayed in bright green color;
The while(1) loop repeats the function call and the printing processes after setting the cursor position to position (0,0) to overwrite the old values and refresh the printed values every 50 milliseconds.

The function works as planed but the results aren't the expected results. Such that whenever I press any key[s] on the keyboard, nothing changes all values are always the same!
So what am I doing wrong here? if this function doesn't retrieve the state of all the keyboard buttons, then what else it does? and how to use it?
___________________________Thanks for your time.
Somehow it just worked after inserting a meaningless call to GetKeyState() before the GetKeyboardState() in the loop!

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
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    COORD cp;
    cp.X=0;
    cp.Y=0;
    BYTE k[256];
    system("MODE CON: COLS=49 LINES=17");

    while(1)
    {
        GetKeyState(0);
        GetKeyboardState(k);
        for(unsigned char y=0; y<16; y++)
        {
            for(unsigned char x=0; x<16; x++)
            {
                if( k[(y*16)+x] > 1 )     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10);
                else if( k[(y*16)+x] > 0 )SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),9);
                else                      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);

                if(k[(y*16)+x])cout<<"X  ";
                else cout<<"0  ";
            }
            cout<<endl;
        }
        Sleep(50);
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cp);
    }
}


The problem was solved but since I didn't understand how?! then it's not solved yet!
Topic archived. No new replies allowed.