Python中用户空闲时间的检测

2024-09-27 23:21:35 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的Python代码应该打印出自上一次用户活动(鼠标移动、键盘按键按下)后经过的时间

from ctypes import Structure, windll, c_uint, sizeof, byref
import time

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



for i in range(10):
    print get_idle_duration()
    time.sleep(1)

问题:但是,如果在脚本执行过程中没有用户输入,则运行此脚本将输出以下结果:

^{pr2}$

为什么打印出来的空闲时间没有继续增加,而是看起来像是在不断地重置?在


Tags: 用户importgettime时间structureuintidle

热门问题