How to detect 'Idle' status in Mac (Qt\C++ application)


Hi,

I have a C++/Qt application for Win & Mac where I need to know if the user is in an 'idle' status (no user interaction in X minutes).

In Windows I am using GetLastInputInfo and it is working perfectly:

....

// Get the last input event info.
LASTINPUTINFO li;
li.cbSize = sizeof(LASTINPUTINFO);
::GetLastInputInfo(&li);

// Calculate the time elapsed in seconds.
DWORD te = ::GetTickCount();

int elapsed = (te - li.dwTime) / 1000;

....

For Mac I have read that CGEventSourceCounterForEventType could help me. But I can not find how to use it in a C++ method

I would thank any help,

Diego
My suggestion would be a continuous timer every x minutes. In the timer function you can use GetLastInputInfo(...) or CGEventSourceCounterForEventType check whether it is due or not.

If you want it more exact you may calculate the

timepoint of the last user input + x minutes - now

which is the duration for the next timer event. Or if (timepoint of the last user input + x minutes) is less than now it is due.
the OS tracks this, somehow, somewhere. there is probably a way to ask for it. Which may be what you are showing, but I don't know how to use it... All modern OS track this to log you out after a bit or fire up a screen saver or whatever.
Last edited on
Topic archived. No new replies allowed.