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;
}
QFrame
class is designed as a simple frame object. It does not works with any input by default. So you must specifify explicitly a focus policy which allows to retrieve keyboard input events using QWidget::setFocusPolicy()
method. QFrame
by default has Qt::NoFocus
policy. Try to set frame's focus policy
property to Qt::StrongFocus
and launch your program again.
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;
}
I find the fact the keys W, A, S, and D are typical game movement keys and also the keys you are having issues with to be a very suspicious coincidence. My best guess is you have some kind of "gaming" keyboard, system extension or custom driver. I'm thinking of things like a "virtual joystick", "power keys", VR software, macro software, etc. I'd be more confident in that answer if it wasn't happening on both a Mac and a PC (unless you use the same keyboard or other hardware). Either way I'd try another keyboard and start the system in safe mode with other programs closed just to make sure these key events aren't being intercepted/modified at the system level.
As first please do what Meefte said (Provide a Minimal, Complete, and Verifiable example"). And take a look at your other programms running. Did you allready check that something blocks it? Try your code on another computer or deactivate any tool you installed for your keyboard. I guess something else blocks your input. Best regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With