How do I detect if the system is idle in windows using python. i.e. no keyboard or mouse activity. This has already been asked before. And there seems to be no GetLastInputInfo in pywin32 module.
+1
A:
Actually, you can access GetLastInputInfo
via the cytpes
library:
import ctypes
GetLastInputInfo = ctypes.windll.User32.GetLastInputInfo # callable function pointer
This might not be what you want though, as it does not provide idle information across the whole system, but only about the session that called the function. See MSDN docs.
Alternatively, you could check if the system is locked, or if the screen-saver has been started.
jcoon
2009-05-26 18:15:37
+3
A:
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.
FogleBird
2009-05-26 19:17:19
A:
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 time
import win32api
for 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.
Joma
2010-08-14 14:06:35