Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting idle time using python

Tags:

python

winapi

How do I detect if the system is idle on Windows using Python (i.e. no keyboard or mouse activity). This has already been asked before, but there doesn't seem to be a GetLastInputInfo in the pywin32 module.

like image 575
Badri Avatar asked May 26 '09 17:05

Badri


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) .


2 Answers

from ctypes import Structure, windll, c_uint, sizeof, byref  class LASTINPUTINFO(Structure):     _fields_ = [         ('cbSize', c_uint),         ('dwTime', c_uint),     ]  def get_idle_duration():     lastInputInfo = LASTINPUTINFO()     lastInputInfo.cbSize = sizeof(lastInputInfo)     windll.user32.GetLastInputInfo(byref(lastInputInfo))     millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime     return millis / 1000.0 

Call get_idle_duration() to get idle time in seconds.

like image 200
FogleBird Avatar answered Sep 28 '22 02:09

FogleBird


import win32api def getIdleTime():     return (win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0 
like image 33
Derek Avatar answered Sep 28 '22 02:09

Derek