python控制滑鼠鍵盤+監聽鍵盤
阿新 • • 發佈:2021-06-29
import pyautogui from pynput.keyboard import Controller, Key, Listener # 監聽按壓 def on_press(key): try: print("正在按壓:", format(key.char)) except AttributeError: print("正在按壓:", format(key)) # 監聽釋放 def on_release(key): print("已經釋放:", format(key)) if key == Key.esc: # 停止監聽 quit() # 開始監聽 def start_listen(): with Listener(on_press=on_press, on_release=on_release) as listener: listener.join() if __name__ == '__main__': # 例項化鍵盤 kb = Controller() print(pyautogui.size()) print(pyautogui.position()) pyautogui.hotkey('alt', 'tab') i = 0 pyautogui.hotkey('ctrl', 'v') # while i < 20: # pyautogui.hotkey('ctrl', 'v') # if i>=10: # pyautogui.press(str(int(i/10))) # pyautogui.press(str(i%10)) # else: # pyautogui.press(str(i)) # pyautogui.hotkey('alt', 's') # i += 1 #開始監聽,按esc退出監聽 start_listen()