如何知道 Pynput 中 on_press 和 on_release 之间的时间间隔?

问题描述

我的 pynput 有问题,我不知道如何设置 on_presson_release间的时间间隔。

例如,当我按下“e”并按住 5 秒钟后松开时,代码显示我按住了“e”按钮 5 秒钟。

from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Key,Listener as KeyboardListener
from pynput import keyboard
import logging
import time


logging.basicConfig(filename="event_log.py",filemode="w",level=logging.DEBUG,format="%(message)s")
logging.info("import pyautogui as pg\nimport time\nimport keyboard\n\nwhile keyboard.is_pressed('q') == False:")


def on_press(key):
    if key == Key.esc:
        # Stop listener
        mouse_listener.stop()
        return False


def on_release(key):
    keyBefore = key
    print("{0} release".format(key))
    logging.info("{0} release".format(key))


def on_move(x,y):
    logging.info("Mouse moved to ({0},{1})".format(x,y))
    print("Mouse moved to ({0},y))


def on_click(x,y,button,pressed):
    if pressed:
        logging.info("Mouse clicked at ({0},{1}) with {2}".format(x,button))


keyboard_listener = KeyboardListener(on_press=on_press,on_release=on_release)
mouse_listener = MouseListener(
    on_move=on_move,on_click=on_click,)


def main():
    keyboard_listener.start()
    mouse_listener.start()
    keyboard_listener.join()
    mouse_listener.join()


if __name__ == "__main__":
    main()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)