@FogleBird的答案非常酷,而且工作很快,但我不知道它是如何工作的,所以這裏有一個測試示例。線程正在啓動,每10秒尋找最後一次空閒時間。如果在此時間窗口內進行任何移動,它將被打印出來。
from ctypes import Structure, windll, c_uint, sizeof, byref
import threading
//Print out every n seconds the idle time, when moving mouse, this should be < 10
def printit():
threading.Timer(10.0, printit).start()
print get_idle_duration()
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
printit()
由於這是之前問,你爲什麼又要求?你認爲什麼改變會產生不同的答案? – 2009-05-26 17:41:24
也許現在有人可以回答這個問題,但這個老問題埋在了年代和默默無聞之中。你怎麼能「碰撞」別人的舊問題? – 2009-05-27 02:42:38