1

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()
asked Mar 4, 2025 at 1:01
6
  • Please refer to this issue on GitHub regarding usage of pynput in Python 3.13. Commented Mar 4, 2025 at 1:17
  • what means not responding to key events as expected? I doesn't work as all? or it doesn't work with some keys? Or it work in slow or something else? We can't see your computer, we can't read in your mind - you have to describe all details in question (not in comments) Commented Mar 4, 2025 at 10:05
  • I don't know how it works on Arch but on Linux Mint it raise error and it needs to run as root because it need access to some /dev/.... Probably it can be changed with some settings. (Platform limitations — pynput 1.7.6 documentation). But modules like keyboard or mouse don't have this problem Commented Mar 4, 2025 at 10:10
  • @furas Not responding to key events as expected means that my script doesn't react at all when I press any key. I run the script on my PC and no matter what key I press—whether it’s space, shift, or any alphanumeric key—I don't see any of the print outputs I added in the code. Commented Mar 4, 2025 at 17:33
  • @furas I tried the keyboard module, but I didn't use it because of the issue of requiring root privileges, which isn't ideal for my case. I'll see if I can change some settings to work around this limitation. Thanks for the tip!s Commented Mar 4, 2025 at 17:37

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.