目录
1.安装pynput
pip install pynput
2.监视
from pynput import keyboard
def on_press(key):
try:
print('摁下 {0} 键'.format(key.char))
except AttributeError:
print('摁下特殊键 {0}'.format(key))
#通过属性判断按键类型。
def on_release(key):
print('{0} 松开'.format(key))
if key == keyboard.Key.esc:
return False#esc键退出程序
with keyboard.Listener(on_press=on_press,on_release=on_release) as listener:listener.join()
3.按键检测(常建)
from pynput import keyboard
def on_press(key):
try:
if key.char=='w':
print('按下{}键'.format(key.char))
except:
print('',end='')
with keyboard.Listener(on_press=on_press) as listener:listener.join()
4.按键检测(特殊建)
from pynput import keyboard
def on_press(key):
try:
if key=='Key.alt_l':#左alt
print('按下{}键'.format(key))
except:
print('',end='')
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
也可以用监视查找