xlib XNextEvent检查是否按下了某个键

时间:2016-07-31 10:30:57

标签: c++ linux c++11 x11 xlib

我正在使用xlib来获取键盘输入我想模拟windows的getAsynckeystate()以检查按钮是否被按下我尝试使用计时器来修复结果但仍然打破了。如果' z'该函数应始终返回true。即使同时按下或释放其他键(现在不工作),也会按下

以下代码

bool KeyboardState::keyPressed(Display* d, Window curFocus,int revert, Window root) {
XEvent ev;
XNextEvent(d, &ev);
clock_t startTime;
switch (ev.type) {
    case FocusOut:
        if (curFocus != root)
            XSelectInput(d, curFocus, 0);

        XGetInputFocus(d, &curFocus, &revert);
        printf("New focus is %d\n", (int) curFocus);

        if (curFocus == PointerRoot)
            curFocus = root;

        XSelectInput(d, curFocus, KeyReleaseMask | FocusChangeMask | KeyPressMask);
        break;

    case KeyPress:
        ks = XLookupKeysym(&(ev.xkey), 0);

        if (ks == XK_z) {

            keyState = true;
            startTime = clock();
        }
        break;
    case KeyRelease:
        if(ks == XK_z && startTime - clock() > 0){

        ks = XLookupKeysym(&(ev.xkey), 0);
            keyState = false;
        }
}
return keyState;
}

1 个答案:

答案 0 :(得分:0)

n.m链接以下功能,接受为答案:

  

tronche.com/gui/x/xlib/input/XQueryKeymap.html