[Python] How to clear keyboard event buffer pynput.keyboard

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function that is called on_press. However, if the user constantly hits the key the keyboard event buffer queue gets really large and my function (which takes a few hundreds of ms) gets called even after the user has stopped pressing the key. How can I solve this issue?

    from pynput import keyboard

    def f1():
    print("starting f1()..")
    # f1 takes time to finish

    def on_press(key):
    print("pressed some key")
    f1()

    with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

    Continue reading...

Compartilhe esta Página