python listen to keyboard input

Solutions on MaxInterview for python listen to keyboard input by the best coders in the world

showing results for - "python listen to keyboard input"
Noemi
05 Oct 2019
1# For windows
2from pynput import keyboard
3
4def on_press(key):
5    try:
6        k = key.char  # single-char keys
7    except:
8        k = key.name  # other keys
9    print('Key pressed: ' + k)
10    return False  # stop listener; remove this if want more keys
11
12listener = keyboard.Listener(on_press=on_press)
13listener.start()  # start to listen on a separate thread
14listener.join()  # remove if main thread is polling self.keys
15