"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> Keyboard control .py>> Author: Liu Yang>> e-mail: [email protected]>> blog: Www.cnblogs.com/liu66blog "" " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" #!/usr/bin/env python#-*-. Coding:utf-8-*-import sys, osfrom pynput.keyboard import controller,key,listener# Monitor Press def on_press (Key): try: Print ("Pressing:", Format (Key.char)) except Attributeerror:print ("pressing:", Format (key)) # Listener release def on_release (key): P Rint ("Already released:", Format (key)) if Key==key.esc: # Stop monitoring return false# start monitoring Def start_listen (): With Listener ( On_press=on_press,on_release=on_release) as Listener:listener.join () if __name__ = = ' __main__ ': # Instantiate keyboard Kb=co Ntroller () # Use the keyboard to enter a letter kb.press (' a ') kb.release (' a ') # using the keyboard input string, note that the current keyboard is tuned into English kb.type ("Hello World") # using Key.xxx input kb.press (key.space) # Start listening, press ESC to exit the listenerStart_listen ()
[Python Study Notes] for keyboard control and monitoring