Intercept QML keys on C++ side.
-
Hi all. Is it possible to intercept keyboard events on C++ side with UI in QML? I've already tried to install event filter on whole
QApplication::instance()
and on QQMLEngine particulary, it didn't help to catch keyboard events.My
main.cpp
looks like:int main(int argc, char* argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; engine.installEventFilter(new KeysInterceptor()); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
Interceptor:
class KeysInterceptor : public QObject { Q_OBJECT public: explicit KeysInterceptor(QObject *parent = nullptr); // QObject interface public: virtual bool event(QEvent *event) override; virtual bool eventFilter(QObject *watched, QEvent *event) override; };
-
You can do it. You can install filter on the app itself & try to do something. You can do is one part. I'm wondering any reason to get into this hack ?
-
@dheerendra As i said, i tried already to install event filter for whole application, but
QKeyEvent
s not appear. I need to black the screen by timer, that count after last key was pressed. In other words - if device is idle for some time, screen should be shutdown. -
How do you find that device is idle ? Can you explain ? Based on this there can be another solution.
Coming to filters -
We can make it work. Show me your code inside the eventFilter. Did you install the filter for QApplication object ? -
@dheerendra Every key press should reset timer inside filter object. Once timer fired - signal generated and device black out screen.
Interceptor:
bool KeysInterceptor::event(QEvent *event) { qDebug() << __FUNCTION__ << ":" << event->type(); return QObject::event(event); } bool KeysInterceptor::eventFilter(QObject *watched, QEvent *event) { qDebug() << __FUNCTION__ << ":" << watched << " - " << event->type(); return QObject::eventFilter(watched, event); }
-
with this code for which object you have installed the filter ? Is it for engine or app object ?
-
@dheerendra I tried both ways below:
QApplication::instance()->installEventFilter(new KeysInterceptor);
QQmlApplicationEngine engine; engine.installEventFilter(new KeysInterceptor);
-
@egor.utsov said in Intercept QML keys on C++ side.:
app
Just try..
KeysInterceptor *keyI = new KeysInterceptor; app.installEventFilter(keyI);
Remove event(...) method.
-
@dheerendra
main.cpp:QApplication app(argc, argv); KeysInterceptor *ki = new KeysInterceptor; app.installEventFilter(ki);
KeyInterceptor.cpp:
bool KeysInterceptor::eventFilter(QObject *watched, QEvent *event) { if (event->type() == QEvent::Type::KeyPress || event->type() == QEvent::KeyRelease) qDebug() << __FUNCTION__ << ":" << watched << " - " << event->type(); return QObject::eventFilter(watched, event); }
-
@dheerendra i just checked it on desktop and it works like a charm
-
This should work. What is the issue ? Sorry i missed seeing your response.
Question - To blackout your screen are you dependent on the key events ? -
@dheerendra yes. Blackout should happen if no activity on keyboard is observed for ten minutes for example
-
You can do this with simple timer which fires after 10 minutes. Inside the handler just check whether key event happened. I'm not in favor of event filter in QApplication becz, every event goes there, checks & then responds. It is risky & performance hit.
-
@dheerendra how i can save time of last key event in this case?
-
you can use the following
Window { visible: true width: 640 height: 480 title: qsTr("Hello World") property date startDate :new Date(); property int mils; Timer{ id : tim interval: 10000 repeat: true running: true onTriggered: { var end = new Date(); var elapsed = end.getTime() - startDate.getTime() if (elapsed>=10000){ console.log("Blackout") }else { console.log("No blackout") startDate = new Date() } } } Rectangle{ anchors.fill: parent focus: true objectName: "Dheerendra" Keys.onPressed: { console.log("Key is pressed") startDate = new Date(); } } }
-
@dheerendra i have many places, where key processed. It is hard to put such code everywhere
-
It can be componetized as well. If it is everywhere, catch at the source only with qApp.
-
@dheerendra once again - key events does not appear in filter. Think it is worth to mention that i use linuxfb plugin withOUT libinput.
-
basic question - In your program are you able to catch/handle the keyboard events ?
-
@dheerendra qml ui react to key presses.