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.
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) .
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.
import win32api def getIdleTime(): return (win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With