Important: Please read the Qt Code of Conduct -

Block key events for entire application

  • Hi there.
    Is there is any way to block all key events for entire application?

    I know that i can filter events to all of my QWidgets by overiding

    ::event(QEvent* event)

    Problem is i have a lot of widgets (about 15 classes) and cant override event(...) for all of them.

    Can i forbid all key events for entire app?

  • Lifetime Qt Champion


    You can setup an event filter on the application object where you drop all keyboard related events.

    Or you can disable all widgets.

    Depends on what your application is supposed to do and look like.

  • Thanks for @SGaist , i dont know that filter can be applied to every QObject.

    So current solution is:

    class EventConsumer : public QObject
        EventConsumer(QObject *parent);
        bool eventFilter(QObject *obj, QEvent *event);
    bool EventConsumer::eventFilter(QObject *obj, QEvent *event)
        if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            qDebug("Ate key press %d", keyEvent->key());  // Here you can generate consumed signal or just consume event
            return true;
            // standard event processing
            return QObject::eventFilter(obj, event);

    And in main.cpp add code:

    QApplication app(argc, argv);
    EventConsumer* event_consumer = new EventConsumer(nullptr);

    Works fine!

Log in to reply