Important: Please read the Qt Code of Conduct -

Capture all user inputs (screensaver)

  • I have a timer that will cause a screensaver to start after a certain amount of time of no user activity. I need a way to restart the timer when the user initiates any kind of input: key press or mouse event. I wanted to know if there is a global signal I can connect to that will send me a signal whenever there is a user event. I also need that user event to be sent to the qml element that is in active focus. I am trying to avoid putting a restart timer call in all the button on the screen.

  • You can install an event filter ("QObject::installEventfilter() ": your QApplication object. You can react on the events you are interested in an send them further to their actual recipient(s). I'm not a QML export, but I guess that you will have to add this in a small C++ class.

  • I guess it can be something like this: you create a class, let's call it UserInputDetector that installs an event filter for the QApplication object. Inside the filter, you catch all events that make the timer restart and emit a signal when the events occur. Then, you put an instance of this class in a plugin and use it inside QML code (e.g, catching the signal to act appropriately):

    @UserInputDetector::UserInputDetector(QObject *parent) :

    bool UserInputDetector::eventFilter(QObject *obj, QEvent *event)
    switch (event->type()) {
    case QEvent::KeyRelease:
    case QEvent::KeyPress:
    case QEvent::MouseButtonPress:
    case QEvent::MouseMove:
    case QEvent::MouseButtonRelease:
    emit userEventOccurred();
    return false;

    And the plugin:
    @void UserInputDetectorPlugin::initializeEngine(QDeclarativeEngine *engine, const char *uri)
    engine->rootContext()->setContextProperty("userInputDetector", new UserInputDetector(engine));

    Q_EXPORT_PLUGIN2(userInputDetectorPlugin, UserInputDetectorPlugin);@

    And inside QML:
    @Connections {
    target: userInputDetector
    onUserEventOccurred: timer.restart();

  • Thanks,
    That is exactly what I needed.

  • How did that turn out? I was also thinking of trying to write a screen saver, but did you make an open-source project of this already?

Log in to reply