Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Block key events for entire application
Qt 6.11 is out! See what's new in the release blog

Block key events for entire application

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goldstar2154
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • G Offline
        G Offline
        goldstar2154
        wrote on last edited by
        #3

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

        So current solution is:

        class EventConsumer : public QObject
        {
            Q_OBJECT
        
        public:
            EventConsumer(QObject *parent);
            ~EventConsumer();
        
        protected:
            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;
            }
            else
            {
                // standard event processing
                return QObject::eventFilter(obj, event);
            }
        }
        

        And in main.cpp add code:

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

        Works fine!

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved