Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the display idle time from Python in Windows, Linux, and MacOS?

I would like to know how long it's been since the user last hit a key or moved the mouse - not just in my application, but on the whole "computer" (i.e. display), in order to guess whether they're still at the computer and able to observe notifications that pop up on the screen.

I'd like to do this purely from (Py)GTK+, but I am amenable to calling platform-specific functions. Ideally I'd like to call functions which have already been wrapped from Python, but if that's not possible, I'm not above a little bit of C or ctypes code, as long as I know what I'm actually looking for.

On Windows I think the function I want is GetLastInputInfo, but that doesn't seem to be wrapped by pywin32; I hope I'm missing something.

like image 984
Glyph Avatar asked Oct 19 '08 23:10

Glyph


People also ask

How does Python determine IDLE time?

Call get_idle_duration() to get idle time in seconds. Another library could set argtypes , restype , or errcheck on windll. user32. GetLastInputInfo in an incompatible way, so you should use your own WinDLL instance, e.g. user32 = WinDLL('user32', use_last_error=True) .


1 Answers

Gajim does it this way on Windows, OS X and GNU/Linux (and other *nixes):

  1. Python wrapper module (also includes Windows idle time detection code, using GetTickCount with ctypes);
  2. Ctypes-based module to get X11 idle time (using XScreenSaverQueryInfo, was a C module in old Gajim versions);
  3. C module to get OS X idle time (using HIDIdleTime system property).

Those links are to quite dated 0.12 version, so you may want to check current source for possible further improvements and changes.

like image 128
drdaeman Avatar answered Oct 14 '22 10:10

drdaeman