pythonpython-3.xkeyboard-eventskeylistenerpynput

Pynput Listener is not calling on_press function


I'm using pynput.keyboard.Listener to listen for keyboard input. The function that is passed to its on_press or on_release arguments is never called after starting the Listener thread although the thread is running (checked with its ìs_alive() method).

A minimal example as it is also given by the pynput documentation is (for non-blocking operation):

import pynput.keyboard as kb

def say(key):
    print(key)

lis=kb.Listener(on_press=say, on_release=say)
lis.start()

while True:
    pass

the function say is never called, neither on pressing or releasing any key. lis.is_alive() returns True after starting it. No Error is shown or Exception raised. Running the Listener blocking with lis.join() doesn't help either.


Solution

  • The problem was solved by switching from Wayland to X11 on the Ubuntu 22.04 machine.

    Apparently no key presses were ever registered by the Listener while using Wayland.