Is GetKeyboardState() function is toggle based?

Viewed 32

I am just trying to get the all keyboard keys state. For that I wrote this simple program for trial.

I have made a separate thread for updating keystate.

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <pthread.h>

BYTE keys[256];

void *Thread(void *vargp);
void gotoxy(int x, int y);

COORD coord;

int main(){
   pthread_t KEYSThread;
   pthread_create(&KEYSThread,NULL,Thread,NULL);
    while(1){
            for(int i=0x41;i<=0x5A;i++){
                printf("Key State %c: %d\n",i,keys[i]);
            }
            if(keys[0x31]){
                if(keys[0x32]){
                    exit(0);
               }
            }
    
        gotoxy(0,0);

    }

}

void *Thread(void *vargp){
    while(1){
        GetKeyState(0);   
        //This is done to update the Keyboard buffer 
        //which in many cases windows won't do itself .. might be some bugs
        GetKeyboardState(keys);
        
    }
}

void gotoxy(int x, int y)
{
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

Now when running initially it is all set to zero. It shows the state of Keys A-Z keys. As I press the keys the keys[] becomes 1 which is desirable but after I lift it should have gone to 0 but seems that is not the case. Only after I press the keys again will the keys[i] go to 0.

It seems like GetKeyboardState() is identifying every keys as toggle buttons. Can any one help me with this problem.

If you can suggest me some alternatives to GetKeyboardState(), please present a link to where I can learn about it (or syntax of the command).

OTHER THAN GetAsyncKeyState(),etc. I want the state data to be transferred in a bulk rather than one by one.

0 Answers
Related