Qt全局监听事件

class KeyPressFilter : public QObject
{
 protected:
    bool eventFilter(QObject *obj, QEvent *event) override
    {
        if(event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast(event);
            qDebug("key press %d", keyEvent->key());
        }
        return QObject::eventFilter(obj, event);
    }
};
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    KeyPressFilter keyPressFilter;
    app.installEventFilter(&keyPressFilter);
    ...
    return app.exec();
}

你可能感兴趣的:(Qt,qt)