python监听键盘事件

python监听键盘事件

pip install pynput

 

 

from pynput import keyboard
 
def on_press(key):
 try:
  print('alphanumeric key {0} pressed'.format(key.char))
 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
 
while True:
 with keyboard.Listener(
  on_press = on_press,
  on_release = on_release) as listener:
  listener.join()

 

posted @ 2020-03-07 19:24  newmiracle宇宙  阅读(1442)  评论(0编辑  收藏  举报