Answer the question
In order to leave comments, you need to log in
Qt: how to find system idle time?
Good day.
How to implement it? That is, if the user is not active for some time (left), then do some action.
Interested in the implementation at the global level, that is, to work even when the application is minimized or in the tray or the application window is not in focus. It is supposed to monitor the keyboard and mouse. Inside the widget, this can be done by overriding mouseMoveEvent and keyPressEvent. But at the global level, I have no idea how.
Answer the question
In order to leave comments, you need to log in
Win:
LASTINPUTINFO LastInput = {};
LastInput.cbSize = sizeof(LastInput);
::GetLastInputInfo(&LastInput);
uint IdleTime = (::GetTickCount() - LastInput.dwTime)/1000;
Lin:
int EventBase = 0;
int ErrorBase = 0;
uint IdleTime = 0;
if (::XScreenSaverQueryExtension(m_pDisplay, &EventBase, &ErrorBase))
{
XScreenSaverInfo* pInfo = ::XScreenSaverAllocInfo();
if (pInfo)
{
::XScreenSaverQueryInfo(::XOpenDisplay(NULL), DefaultRootWindow(m_pDisplay), pInfo);
IdleTime = static_cast<uint>(pInfo->idle)/1000;
::XFree(pInfo);
}
}
Mac:
CFTimeInterval Interval = CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateHIDSystemState,
kCGAnyInputEventType);
return static_cast<uint>(Interval);
Please.
Qt does not know how, I also looked for it earlier. Read information on SetWindowsHookEx if under Windows.
there is a keepassx program, you can try to look in its sources - there is an option "lock the password database after n seconds of inactivity"
Alternatively, you can use the task scheduler built into the operating system.
Under Windows, you can create a task in the scheduler that will be executed when the system goes into an idle state, for sure there is something similar in Linux and Mac.
Of course, you can do it manually, but it's not so simple and you need an integrated approach:
processor, disk, network, user input, SetThreadExecutionState , full-screen, applications, etc.
For example, if the user is watching a movie, then this is not idle, and the approach proposed by the ixSci user will at least not work correctly. There are also situations when the processor is idle, but disk operations are actively performed, or the network is actively working.
It is also useful to lower the priority of the background task.
For Windows, see SetThreadPriority with the THREAD_MODE_BACKGROUND_BEGIN flag.
Also, if approached thoroughly, then it is necessary to track the exit from idle, and stop the task.
There is also a QDesktopWidget class, perhaps it tracks all global events, try it.
github.com/euroelessar/qutim/tree/master/core/src/corelayers/idledetector
There is working code here for Windows/MacOS X/X11, tested, working, api on Qt :)
Initially taken from Psi, then slightly modified. There is no more general solution, only implementations for a specific platform
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question