在工作中,我们必须进行自己的时间管理,并要不时加以控制。因为我总是忘记,所以当我休息和休息多长时间时,我决定编写一个python脚本,该脚本在启动时运行,并在5分钟未移动鼠标或在键盘上键入后写当前时间。
import datetime
def writetime():
t = datetime.datetime.now()
with open("C:\\Users\\[USER]\\Desktop\\time.txt", 'a') as f:
f.write('%s \n' % t)
我只是不知道,自从上次输入起经过一定时间后,如何执行函数的写入时间。
答案 0 :(得分:1)
pynput
似乎适合您。 See docs
那会是
from pynput import mouse
with mouse.Listener(on_click=reset_timer,
on_move=reset_timer,
on_scroll=reset_timer) as listener:
begin_timer()
答案 1 :(得分:0)
另一种方法可能是在5分钟后关闭显示器屏幕(屏幕保护程序选项),然后编写一个脚本来检测显示器状态。
以下是有关如何执行此操作的示例: How to check whether the device's display is turned on/off with Python?
快乐编码
答案 2 :(得分:0)
这可能不是最干净的解决方案,但是由于我是Python新手,所以我对此非常满意。 导入日期时间 从ctypes导入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
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更改为希望的时间(以秒为单位)