python listen to keyboard input linux

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

showing results for - "python listen to keyboard input linux"
Dean
16 Jan 2020
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
Lily
31 Apr 2020
1# For Linux
2import keyboard  # using module keyboard
3while True:  # making a loop
4    try:  # used try so that if user pressed other than the given key error will not be shown
5        if keyboard.is_pressed('q'):  # if key 'q' is pressed 
6            print('You Pressed A Key!')
7            break  # finishing the loop
8    except:
9        break  # if user pressed a key other than the given key the loop will break
10