赞
踩
目录
pip install pynput
- 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()
-
- 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()
- 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()
左alt=Key.alt_l
右alt=Key.alt_gr
左ctrl=Key.ctrl_l
右ctrl=Key.ctrl_r
空格=Key.space
也可以用监视查找
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。