detecting idle time using python
from ctypes import Structure, windll, c_uint, sizeof, byrefclass 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.
Seems like GetLastInputInfo
is now available in pywin32:
win32api.GetLastInputInfo()
does the trick and returns the timer tick from the last user input action.
Here with an example program
import timeimport win32apifor i in range(10): print(win32api.GetLastInputInfo()) time.sleep(1)
If one presses a key/moves the mouse while the script sleeps, the printed number changes.