0

在工作中,我们必须进行自己的时间管理,并时不时地得到控制。因为我总是忘记,当我休息多长时间时,我决定编写一个在启动时运行的 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)

我只是不知道,自上次输入以来经过一定时间后如何执行我的函数 writetime。

4

3 回答 3

1

pynput看起来它可能适合你。查看文档

会是这样的

from pynput import mouse
with mouse.Listener(on_click=reset_timer,
    on_move=reset_timer, 
    on_scroll=reset_timer) as listener:
    begin_timer()
于 2019-03-08T09:46:33.310 回答
0

它可能不是最干净的解决方案,但由于我是一名新手 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 更改为希望的时间(以秒为单位)

于 2019-03-08T13:49:12.950 回答
0

另一种方法可能是在 5 分钟后关闭显示器屏幕(屏幕保护程序选项),然后编写一个检测显示器状态的脚本。

这是一个如何执行此操作的示例: 如何使用 Python 检查设备的显示是否打开/关闭?

快乐编码

于 2019-03-08T10:15:47.367 回答