Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Key Event Filter for application



  • Hello.
    I have a big apllication with a lot of widgets. And i have a task to filter all autorepeate key press.
    I registerered eventFilter() for application:

    qApp->installEventFilter(this);
    

    but it doest work. it filters events when they ignored by other widgets.
    how can i filter key events before they riches cild wodgets?


  • Qt Champions 2017

    Not a good ideal to place the filter for qApp itself. What the is done in the filter method ? Can you show it ?



  • i need to filter repeatedly pressed buttons. I tryed do it like this:

    bool ApplicationController::eventFilter(QObject *obj, QEvent *event)
    {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            int key = keyEvent->key();
            if(key==Qtv::Key_Ok && keyEvent->isAutoRepeat()){
                return true;
            }
        }
        return false;
    }
    

    but it filters only if other widgets ignores. i need to filter before other widgets.


  • Lifetime Qt Champion

    Hi
    You can do it with notify override

    #include <QApplication>
    #include <QKeyEvent>
    class Application final : public QApplication
    {
    public:
        Application(int &argc, char **argv) : QApplication(argc, argv) {}
        virtual bool notify(QObject *receiver, QEvent *event) override
        {
            if (event->type() == QEvent::KeyPress) {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                int key = keyEvent->key();
                if ( keyEvent->isAutoRepeat()) {
                    return true;
                }
            }
           return QApplication::notify(receiver,event);
        }
    };
    

    Credits to dude on stackoverflow.


Log in to reply