使用python检测空闲时间 [英] detecting idle time using python

查看:57
本文介绍了使用python检测空闲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Python 检测系统在 Windows 上是否空闲(即没有键盘或鼠标活动).这已经被问到 之前,但 pywin32 模块中似乎没有 GetLastInputInfo.

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.

推荐答案

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

调用 get_idle_duration() 以秒为单位获取空闲时间.

Call get_idle_duration() to get idle time in seconds.

这篇关于使用python检测空闲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆