QT, when the user is idle from the computer?

I am trying to figure out how to track when a user was idle from a computer, which means not only my application. The reason is because I want my application to be able to set the user as "Away" after a certain time. Think of it as Skype, which takes you in X minutes.

Any ideas how to do this?

Edit

What I still have to track the mouse:

    //Init
    mouseTimer = new QTimer();
    mouseLastPos = QCursor::pos();
    mouseIdleSeconds = 0;

    //Connect and Start
    connect(mouseTimer, SIGNAL(timeout()), this, SLOT(mouseTimerTick()));
    mouseTimer->start(1000);

void MainWindow::mouseTimerTick()
{
    QPoint point = QCursor::pos();
    if(point != mouseLastPos)
        mouseIdleSeconds = 0;
    else
        mouseIdleSeconds++;

    mouseLastPos = point;

    //Here you could determine whatever to do
    //with the total number of idle seconds.

    qDebug() << mouseIdleSeconds;
}

Can I also add a keyboard to this?

+4
source share
2 answers

There are platform-specific ways to receive user downtime notifications. You should almost always use them, instead of riding yourself.

, . X11, OS X Windows - , . Qt . . .

, , , API-, , .

API- :

  • Windows GetLastInputInfo . .

  • OS X, NSWorkspaceWillSleepNotification NSWorkspaceDidWakeNotification . .

  • X11 API :

    /* gcc -o getIdleTime getIdleTime.c -lXss */
    #include <X11/extensions/scrnsaver.h>
    #include <stdio.h>
    
    int main(void) {
      Display *dpy = XOpenDisplay(NULL);
    
      if (!dpy) {
        return(1);
      }
    
      XScreenSaverInfo *info = XScreenSaverAllocInfo();
      XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
      printf("%u", info->idle);
    
      return(0);
    }
    
+6

.

eventFilter :

QEvent::MouseButtonPress
QEvent::MouseButtonRelease
QEvent::Wheel
QEvent::KeyPress
QEvent::KeyRelease

QTimer, reset events, , , . a >

Edit:
, .

+1

Source: https://habr.com/ru/post/1527839/


All Articles