微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何知道 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 举报,一经查实,本站将立刻删除。