Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xlib XNextEvent checking if a key is held down

I am using xlib to get keyboard input I want to simulate windows its getAsynckeystate() to check if a button is being pressed I tried using a timer to fix the result but its still broken. the function should always return true if 'z' is held down even if other keys are pressed or released at the same time (not working right now)

Code below

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;
}
like image 228
Mercenary_Frank Avatar asked Oct 15 '25 16:10

Mercenary_Frank


1 Answers

n.m linked following function which is accepted as answer:

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

like image 143
Andreas Avatar answered Oct 17 '25 06:10

Andreas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!