[Python] Why is pynput not detecting key presses on my Linux machine?

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to use the pynput library to capture key presses and releases in Python, but the listener is not responding to key events as expected. I’m running the script in a virtual environment on Arch Linux with Python 3.13.

    from pynput import keyboard

    def on_press(key):
    print(f'{key} pressed')

    def on_release(key):
    print(f'{key} released')
    if key == keyboard.Key.esc:
    return False # Stop listener when Esc is pressed

    #Start the listener

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

    Continue reading...

Compartilhe esta Página