from pynput import keyboard # 按下后执行 def on_press(key): try: if key == keyboard.KeyCode.from_char('enter'): pass elif key == keyboard.Key.left: print("1") elif key == keyboard.Key.right: print("2") elif key == keyboard.Key.up: print("3") elif key == keyboard.Key.down: print("3") elif key == keyboard.KeyCode.from_char('q'): exit(-1) except AttributeError: print('special key {0} pressed'.format(key)) # 弹起时执行 def on_release(key): print('{0} released'.format(key)) if key == keyboard.Key.esc: return False if __name__ == '__main__': with keyboard.Listener( on_press=on_press, on_release=on_release) as listener: listener.join()
标签:__,elif,监控,python,Key,键盘,key,keyboard,print From: https://www.cnblogs.com/xiaoerjun/p/16742360.html