I was searching for a simple solution without window focus. Jayk's answer, pynput
, works perfect for me. Here is the example how I use it.
from pynput import keyboard
def on_press(key):
if key == keyboard.Key.esc:
return False # stop listener
try:
k = key.char # single-char keys
except:
k = key.name # other keys
if k in ['1', '2', 'left', 'right']: # keys of interest
# self.keys.append(k) # store it in global-like variable
print('Key pressed: ' + k)
return False # stop listener; remove this if want more keys
listener = keyboard.Listener(on_press=on_press)
listener.start() # start to listen on a separate thread
listener.join() # remove if main thread is polling self.keys
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…