当前位置:   article > 正文

pynput监视键盘_python 键盘 监测

python 键盘 监测

目录

1.安装pynput

2.监视

3.按键检测(常建)

4.按键检测(特殊建)

5.特殊键值

左alt=Key.alt_l

右alt=Key.alt_gr

左ctrl=Key.ctrl_l

右ctrl=Key.ctrl_r

空格=Key.space


1.安装pynput

pip install pynput

2.监视

  1. from pynput import keyboard
  2. def on_press(key):
  3. try:
  4. print('摁下 {0} 键'.format(key.char))
  5. except AttributeError:
  6. print('摁下特殊键 {0}'.format(key))
  7. #通过属性判断按键类型。
  8. def on_release(key):
  9. print('{0} 松开'.format(key))
  10. if key == keyboard.Key.esc:
  11. return False#esc键退出程序
  12. with keyboard.Listener(on_press=on_press,on_release=on_release) as listener:listener.join()

3.按键检测(常建)

  1. from pynput import keyboard
  2. def on_press(key):
  3. try:
  4. if key.char=='w':
  5. print('按下{}键'.format(key.char))
  6. except:
  7. print('',end='')
  8. with keyboard.Listener(on_press=on_press) as listener:listener.join()

4.按键检测(特殊建)

  1. from pynput import keyboard
  2. def on_press(key):
  3. try:
  4. if key=='Key.alt_l':#左alt
  5. print('按下{}键'.format(key))
  6. except:
  7. print('',end='')
  8. with keyboard.Listener(on_press=on_press) as listener:listener.join()

5.特殊键值

左alt=Key.alt_l
右alt=Key.alt_gr
左ctrl=Key.ctrl_l
右ctrl=Key.ctrl_r
空格=Key.space

也可以用监视查找

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/839520
推荐阅读
相关标签
  

闽ICP备14008679号