简体   繁体   English

Python 获得空闲时间

[英]Python get idle duration

im making a script that get idle duration getting the last mouse/kayboard input time and in windows it worked but i canot make it run on linux我正在制作一个获得空闲持续时间的脚本,以获得最后一个鼠标/kayboard 输入时间,并且在 windows 中它工作但我无法让它在 linux 上运行

class LASTINPUTINFO(Structure):
    _fields_ = [
        ('cbSize', c_uint),
        ('dwTime', c_uint),
    ]

def get_idle_duration():
    # get idle time
    lastInputInfo = LASTINPUTINFO()
    lastInputInfo.cbSize = sizeof(lastInputInfo)
    windll.user32.GetLastInputInfo(byref(lastInputInfo))
    millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
    return millis / 1000.0

print(get_idle_duration())

some one can help me to make this code run in linux and windows?有人可以帮我让这段代码在 linux 和 windows 中运行吗?

You can refer idle.py by Gajim你可以参考Gajimidle.py

Just call the getIdleSec() to get the idle time in seconds.只需调用getIdleSec()即可获得以秒为单位的空闲时间。

If you execute idle.py , it will wait for 2.1 seconds and then print the idle time.如果执行idle.py ,它将等待 2.1 秒,然后打印空闲时间。 Later it frees the held data after which if you call the function, it will return zero.稍后它会释放保存的数据,之后如果您调用 function,它将返回零。

The problem with the code given in the question is that it uses windll which is specific to Windows.问题中给出的代码的问题在于它使用了特定于windll的 windll。

If you want to see the script in action in a more detailed manner, you can change main as follows:如果您想以更详细的方式查看运行中的脚本,可以按如下方式更改main

if __name__ == '__main__':
    import time
    for i in range(0,10):
        print(getIdleSec())
    close()

This works on Ubuntu and should work on all Debian based operating systems.这适用于 Ubuntu 并且应该适用于所有基于 Debian 的操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM