'Modifier' key notification (eg. Shift, Ctrl, Alt)
Is there a way of getting an event or slot called when a modifier key such as Shift, Ctrl, or Alt has been pressed regardless of what widget currently has the focus?
So the user presses the left Shift button and an event handler gets called to tell the app that the shift button has been pressed. It doesn't matter if it can't differentiate between left shift and right shift.
It is important that the widget doesn't need focus in order to get the event.
Re: 'Modifier' key notification (eg. Shift, Ctrl, Alt)
Hi there!
I guess you have thought of eventFilters?
Code:
{ Q_OBJECT
public:
{
a->installEventFilter(this);
l->addWidget(a);
b->installEventFilter(this);
l->addWidget(b);
c->installEventFilter(this);
l->addWidget(c);
l->addWidget(statusL);
setLayout(l);
}
protected slots:
void resetStatus()
{
statusL->setText("");
}
protected:
{
if (event
->type
() == QEvent::KeyPress) { QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
if (keyEvent->key() == Qt::Key_Shift)
{
statusL->setText("Shift pressed");
}
if (keyEvent->key() == Qt::Key_Control)
{
statusL->setText("Ctrl pressed");
}
QTimer::singleShot(500,
this,
SLOT(resetStatus
()));
}
// standard event processing
return QObject::eventFilter(obj, event
);
}
private:
};
HIH
Johannes
Re: 'Modifier' key notification (eg. Shift, Ctrl, Alt)
If you want it application wide, why not install an event filter for your app:
Code:
Widget* w = new Widget();
a.installEventFilter(w);
w->show();
return a.exec();
Thats saves you the installation of the event filter for each widget.
Johannes
Re: 'Modifier' key notification (eg. Shift, Ctrl, Alt)
I didn't want to install an application-level event filter as I didn't want to slow down the main message loop, but it seemed that was the only way, so I redesigned the way everything worked and used another method (only pickup the modifier key notification from just certain main widgets).