Search code examples
c++linuxxlib

KeyRelease event never sent


I'm having a little problem catching a key released event to stop my character from walking in my game..

i'm trying to do this:

switch (xev.type)
    {
        case Expose:
        {
            XGetWindowAttributes(dpy, win, &gwa);
            glViewport(0, 0, gwa.width, gwa.height);
        }
        break;

        case KeyPress:
            {
                int key = XLookupKeysym(&xev.xkey, 0);
                if (key == XK_Escape)
                {
                    glXMakeCurrent(dpy, None, NULL);
                    glXDestroyContext(dpy, glc);
                    XDestroyWindow(dpy, win);
                    XCloseDisplay(dpy);

                    running = false;
                    return 0;
                }
                else
                {
                    input->setKey(key, true);
                }
            }
            break;

        case KeyRelease:
            {
                unsigned short is_retriggered = 0;

                if (XEventsQueued(dpy, QueuedAfterReading))
                {
                    XEvent nev;
                    XPeekEvent(dpy, &nev);

                    if (nev.type == KeyPress && nev.xkey.time
                            == xev.xkey.time && nev.xkey.keycode
                            == xev.xkey.keycode)
                    {
                        // delete retriggered KeyPress event
                        XNextEvent(dpy, &xev);
                        is_retriggered = 1;
                    }
                }

                if (!is_retriggered)
                    input->setKey(XLookupKeysym(&xev.xkey, 0), false);
            }
            break;
    }

But I only get the re-triggered key release events, which I don't want. (even though a release/re-press would have the same result, but in the future it might give problems) When i physically release the key, no event is caught.

oh, and input->setKey() basically sets a bool to true (or false) in a std::map, nothing special


Solution

  • This is a common gotcha. If you don't register for specific events (or all), you will not be notified. All of us, doesn't matter the experience, will fall in this one day... :)

    Some usefull links: