它可能不是最干净的解决方案,但由于我是一名新手 Python 程序员,我对此非常满意。从 ctypes 导入 datetime 导入结构、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
while 1:
GetLastInputInfo = int(get_idle_duration())
if GetLastInputInfo >= 10:
start = time.time()
startTime = datetime.datetime.now()
while GetLastInputInfo >= 10:
GetLastInputInfo = int(get_idle_duration())
if GetLastInputInfo < 10:
end = time.time()
time_elapsed = end - start + 10
if time_elapsed >= 10:
with open("C:\\Users\\[USER]\\Desktop\\time.txt", 'w') as f:
f.write('Pause from ' + str(startTime) + ' to ' + str(
datetime.datetime.now()) + '\nDuration: ' + str(time_elapsed))
出于测试目的,我将标记为缺席的时间设置为 10 秒。因此,如果您想做类似的事情,只需确保将所有 10 更改为希望的时间(以秒为单位)