Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6144

Pynput not recognizing inputs when terminal is focused

$
0
0

I'm on Ubuntu 24, using Python 3.12. I have a basic Python script with pynput that stops the program execution when the up arrow is pressed. Running this script in Pycharm has no problems, but in a terminal window it doesn't recognize the inputs unless the terminal is unfocused. Here's my code:

from pynput.keyboard import Key, Listener, KeyCodestop = Falsedef on_press(key):    print(key)    if key == Key.up:        global stop        stop = Trueif __name__ == "__main__":    listener = Listener(on_press=on_press)    listener.start()    while not stop:        pass              # some other code goes here

Replacing listener = Listener(on_press=on_press) with

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

still doesn't work in the terminal until unfocused.


Viewing all articles
Browse latest Browse all 6144

Trending Articles