Search code examples
c++qtkeypressqeventqkeyevent

Qt keyPressEvent not registering when W/A/S/D keys are pressed


I have an app (not related to any game where the W/A/S/D keys may have special meanings for navigation) where there is a QFrame. I overrode the keyPressEvent() to get the text being typed through keyboard while focus in on that QFrame. This is my code:

void MyFrame::keyPressEvent(QKeyEvent *event)
{
    qDebug() << "At least came here" << endl;
    QString text = event->text();
    qDebug() << "Text: " << text << endl;
}

When I type characters from keyboard one at a time, for all characters and numbers, both statements are logged correctly. But for these four keys neither of the log statements are executed, i.e the event handler is not even firing. What is wrong?

Edit: After going though the examples, I tried to form a minimal working example of my bug. This is what I have got. Same problem here as well with doing it through event filter. Only for those four characters it is not logged.

bool MyWidget::eventFilter(QObject *obj, QEvent *event)
{

    if (event->type() == QEvent::KeyPress)
    {
        //this never gets printed
        qDebug() << "Phew!" << endl;
        return true;

    }
    if (qobject_cast<ChildWidget *>(obj) != nullptr)
    {


        ChildWidget *option = qobject_cast<ChildWidget *>(obj);
        if (event->type() == QEvent::Enter || event->type() == QEvent::MouseMove)
        {
            //do stuff
            return true;
        }
        if (event->type() == QEvent::Leave)
        {
            //do stuff
            return true;
        }
        return QWidget::eventFilter(obj, event);
    }
    else
    {
        // pass the event on to the parent class
        return QWidget::eventFilter(obj, event);
    }
}

MyWidget::MyWidget()
{
   //do other initialization
   this->installEventFilter(this);
}

void MyWidget::keyPressEvent(QKeyEvent *event)
{
    qDebug("At least came here");
    QString text = event->text();
    //this prints out whenever I type any character, excpet W/A/S/D
    qDebug() << text;
}

Solution

  • Not sure if I'm misunderstanding something, but the following code is working well and I see all the keys in the log (even capitalized) except key "w".

    Here you have:

    Edit#1: installed an event filter on the QApplication to get the objects which are filtering the events.

    myframe.pro

    TEMPLATE = app
    
    QT     += widgets
    SOURCES += main.cpp \
               myframe.cpp 
    
    HEADERS += myframe.h 
    

    main.cpp

    #include <QtWidgets/QApplication>
    #include <QDebug>
    
    #include "myframe.h"
    
    class QApplicationFilter: public QObject
    {
        public:
            QApplicationFilter(): QObject() {};
            ~QApplicationFilter() {};
    
            bool eventFilter(QObject* obj, QEvent* event)
            {
                qDebug() << "QApplicationFilter: "
                         << obj->objectName()
                         << " - event type: "
                         << event->type();
                return QObject::eventFilter(obj, event);            
            };  
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);    
        a.installEventFilter(new QApplicationFilter());
    
        MyFrame mf;
        mf.show();
        return a.exec();
    }
    

    myframe.h

    #ifndef MYFRAME_H
    #define MYFRAME_H
    
    #include <QtWidgets/QFrame>
    
    class MyFrame : public QFrame
    {
        Q_OBJECT
    
    public:
        MyFrame();
        bool eventFilter(QObject *object, QEvent *event);
    
    protected:
        void keyPressEvent(QKeyEvent *event);
    };
    
    #endif
    

    myframe.cpp

    #include <QDebug>
    #include <QKeyEvent>
    #include "myframe.h"
    
    MyFrame::MyFrame()
    {
       this->installEventFilter(this);
    }
    
    bool MyFrame::eventFilter(QObject *object, QEvent *event)
    {
        if (object == this && event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_W) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
    
    void MyFrame::keyPressEvent(QKeyEvent *event)
    {
        qDebug("At least came here");
        QString text = event->text();
        qDebug() << text;
    }