Why GetKeyboardState() cannot work alone, until GetKeyState(0) is placed beside?

Viewed 28

i use: win10 VS2022 C++ console_application Debug x64

i tryed to use GetKeyboardState() then printf() those 256 keys, but nothing changed from the first print, even if i pressed almost all keys on my keyboard. it seems that something was wrong.(but it didn't return 0, the failure code)

so i added a GetKeyState('O') then printf() it, and pressed 'O', but now both works!

i checked msdn, but i didn't see anything about this side effect of GetKeyState() . i'm not sure this is a bug, or feature, or something else.

#include <iostream>
#include <Windows.h>
int main()
{
    BYTE keybdstate[256] = { 0 },keybdstatelast[256]={0};
    GetKeyboardState(keybdstate);
    memcpy(keybdstatelast, keybdstate, 256);
    for(;;)
    {
        GetKeyboardState(keybdstate);
        for (int i = 0; i < 256; i++)
        {
            if (keybdstate[i] != keybdstatelast[i])
            {
                system("pause");
            }
        }
        memcpy(keybdstatelast, keybdstate, 256);
        Sleep(15);
        GetKeyState(0);//<----if this exist, it would pause when i press any key,
// but if i remove this line, the system never pause. 
//it means, delete this line, GetKeyboardState() didn't work at all.
//however, add this line, GetKeyboardState() would get key state as expected.
//i tryed to use GetKeyState(0) in the first line, but it didn't work.
//how does GetKeyState() influence GetKeyboardState() ?
    }
}
0 Answers
Related