使用python侦听鼠标和键盘事件

时间:2020-03-25 06:09:00

标签: python-3.x pynput

如果键盘或鼠标一段时间未使用,我会尝试提示错误消息。为此,我尝试了pynput模块,这对我来说不值得。谁能帮我这个忙,谢谢

1 个答案:

答案 0 :(得分:0)

如果您不移动(或单击)鼠标并在五秒钟内按下键盘,此代码将显示(xxxxx)。

from pynput import mouse,keyboard
import time
def on_move(x, y):
    global LastTime
    LastTime = time.time()

def on_click(x, y, button, pressed):
    global LastTime
    LastTime = time.time()

def on_scroll(x, y, dx, dy):
    global LastTime
    LastTime = time.time()

def on_press(key):
    global LastTime
    LastTime = time.time()

LastTime = time.time()
listener = keyboard.Listener(on_press=on_press)
listener.start()
listener = mouse.Listener(on_move=on_move,on_click=on_click,on_scroll=on_scroll)
listener.start()
while True:
    if time.time()-LastTime >= 5: # the break time,5 is second
        print("You need break your computer")
        LastTime = time.time()