Search code examples
qt5x11xlib

How to prevent window from loosing focus when receiving a grabbed key from X11


My window calls hide() when a QEvent::FocusOut is received. Simultaniously I want its visibility to be toggled if a hotkey is pressed. Now I have the following problem: Pressing the hotkey, registered with XGrabKex(...) seems to steel the focus of my app. Resulting in an unwanted behaviour. If my app is visible the hotkeyevent steels focus, which results in a QEvent::FocusOut, which hides my app, and after that the hotkey is received which toggles visibility (shows) my app. I.e. my app does not hide when pressing the hotkey.

Is there a way to tell the x window system to not steel the focus when a grabbed key is pressed? Or are there other possible solutions to this problem?


Solution

  • Finally got it to work in a "proper" way:

    bool MainWidget::nativeEvent(const QByteArray &eventType, void *message, long *)
    {
    #ifdef Q_OS_LINUX
        if (eventType == "xcb_generic_event_t")
        {
            xcb_generic_event_t* event = static_cast<xcb_generic_event_t *>(message);
            switch (event->response_type & 127)
            {
            case XCB_FOCUS_OUT: {
                xcb_focus_out_event_t *fe = (xcb_focus_out_event_t *)event;
                if ((fe->mode==XCB_NOTIFY_MODE_GRAB && fe->detail==XCB_NOTIFY_DETAIL_NONLINEAR)
                        || (fe->mode==XCB_NOTIFY_MODE_NORMAL && fe->detail==XCB_NOTIFY_DETAIL_NONLINEAR ))
                    hide();
                break;
            }
            }
        }
    #endif
    return false;
    }