1 """小白随笔,大佬勿喷""" 2 #键盘输入 3 from pynput.keyboard import Key,Controller,Listener 4 keyboard = Controller() 5 keyboard.press("a") #按下a 6 keyboard.release("a") #松开a 7 keyboard.press("A") #按下A 8 keyboard.release("A") #松开A 9 with keyboard.pressed(Key.shift): #按下shift 10 keyboard.press("a") #shift + a 11 keyboard.type("Hello World") #输出文字 12 13 #-------------------------------------------------- 14 15 # 键盘监听 16 from pynput.keyboard import Key,Listener 17 def on_press(key): 18 if key == Key.enter: 19 print('you press Enter') 20 else: 21 return False #按键不是enter,停止监视 22 def on_release(key): 23 if key == Key.enter: 24 print('you release Enter') 25 #监听键盘按键 26 with Listener(on_press=on_press,on_release=on_release) as listener: 27 listener.join() 28 #停止监视 29 Listener.stop()