python 使用python检测空闲时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/911856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detecting idle time using python
提问by Badri
回答by FogleBird
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.
调用get_idle_duration()
以获取空闲时间(以秒为单位)。
回答by Joma
Seems like GetLastInputInfo
is now available in pywin32:
似乎GetLastInputInfo
现在在 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.
如果在脚本休眠时按下一个键/移动鼠标,则打印的数字会发生变化。
回答by Derek
import win32api
def getIdleTime():
return (win32api.GetTickCount() - win32api.GetLastInputInfo()) / 1000.0
回答by Jason Coon
Actually, you can access GetLastInputInfo
via the cytpes
library:
实际上,您可以GetLastInputInfo
通过cytpes
图书馆访问:
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.
不过,这可能不是您想要的,因为它不提供整个系统的空闲信息,而仅提供有关调用该函数的会话的信息。 请参阅 MSDN 文档。
Alternatively, you could check if the system is locked, or if the screen-saver has been started.
或者,您可以检查系统是否已锁定,或者屏幕保护程序是否已启动。
回答by user1767754
@FogleBird's answer is pretty cool and working, but in a hurry i wasn't sure how it works, so a little test example here. A thread is starting, looking for last idle time every 10 seconds. If any movement is made within this time window, it will be printed out.
@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()