python pynputリスニングキーボード

4819 ワード

 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()

 
転載先:https://www.cnblogs.com/py-peng/p/10321543.html