1from pynput.keyboard import Key, Listener
2
3def on_press(key):
4 print('{0} pressed'.format(
5 key))
6
7def on_release(key):
8 print('{0} release'.format(
9 key))
10 if key == Key.esc:
11 # Stop listener
12 return False
13
14# Collect events until released
15with Listener(
16 on_press=on_press,
17 on_release=on_release) as listener:
18 listener.join()
19