C – Use GetKeyState(VK_CAPITAL) & 1 in Linux

Use GetKeyState(VK_CAPITAL) & 1 in Linux… here is a solution to the problem.

Use GetKeyState(VK_CAPITAL) & 1 in Linux

#include <windows.h>

int main() {
if ( ! GetKeyState(VK_CAPITAL) & 1 ) {
printf("caps off");
}
else
printf("caps on");
return 0;
}

But only for Windows

How do I do this with gcc in Linux?

What is the & 1

in GetKeyState(VK_CAPITAL) & 1?

Solution

For the most common scenario for X11-based desktops:

#include <stdio.h>
#include <X11/XKBlib.h>

int main() {
    Display * d = XOpenDisplay((char*)0);

if (d) {
        unsigned n;

XkbGetIndicatorState(d, XkbUseCoreKbd, &n);

printf((n & 1)?" caps on\n":"caps off\n");
    }
}

Make sure you have the X11 development header file and compile:

$ gcc -lX11 test.c -o test

Run it from the console window on the desktop:

$ ./test
caps off
$ ./test
caps on

Related Problems and Solutions