using GetKeyState(VK_CAPITAL) & 1 in linux

让人想犯罪 __ 提交于 2019-12-19 09:19:34

问题


#include <windows.h>

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

but limited to windows only

how to do this in linux with gcc ?

what is & 1 in GetKeyState(VK_CAPITAL) & 1 ?


回答1:


For the most common case of an X11-based desktop:

#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 headers and compile with:

$ gcc -lX11 test.c -o test

Run it from a console window in your desktop:

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


来源:https://stackoverflow.com/questions/9009775/using-getkeystatevk-capital-1-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!