I want to detect whether a key sequence is pressed and want to perform certain task on that event in Qt. Currently I can detect keypresses for certain widgets but how to detect global keypresses. By global I mean that even if the application is minimized or hidden , it should detect key press.
I tried making an eventFilter
for the application, by first overloading QObject::eventFilter
like this:
bool GlobalEventFilter::eventFilter(QObject *Object, QEvent *Event)
{
if (Event->type() == QEvent::KeyPress)
{
QKeyEvent *KeyEvent = (QKeyEvent*)Event;
switch(KeyEvent->key())
{
case Qt::Key_F1:
cout<<"F1 press detected"<<endl;
return true;
default:
break;
}
}
return QObject::eventFilter(Object,Event);
}
and then installing that object as the eventFilter
for my application:
QApplication a(argc,argv);
a.installEventFilter(new GlobalEventFilter());
and I also tried the doing this:
QCoreApplication::instance()->installEventFilter(new GlobalEventFilter());
In both the cases I am able to detect key presses when my application window is open but it fails when window is minimized or hidden. How to solve this?
See Question&Answers more detail:os