Python實現(xiàn)自動化調(diào)用鼠標和鍵盤
自動化鼠標,鍵盤調(diào)用程序用到的模塊是pyautogui。
pyautogui是一個Python第三方庫,用于自動化GUI操作。它可以模擬鼠標和鍵盤的操作,以及屏幕截圖和圖像識別等功能。通過pyautogui,用戶可以編寫腳本來自動化一些重復性的任務,例如自動化測試、數(shù)據(jù)錄入、圖像處理等。
pyautogui支持跨平臺,可以在Windows、macOS和Linux等操作系統(tǒng)上運行。它還提供了豐富的API,包括鼠標和鍵盤操作、屏幕截圖、圖像識別、定時器等功能,可以滿足大部分自動化需求。
寫一個簡單的示例程序,實現(xiàn)鼠標定位在屏幕中央和點擊的效果:
- import pyautogui
- pyautogui.moveTo(pyautogui.size().width / 2,
- pyautogui.size().height / 2)
- pyautogui.click()
和以前一樣,需要在終端里下載模塊:pip install pyautogui
程序源碼如下:
import pyautogui import time # 設置繪圖區(qū)域的大小和位置 x, y = 150, 150 width, height = 500, 500 # 設置繪制的圖形的顏色和線條寬度 color = 'blue' thickness = 5 # 獲取屏幕的大小 screenWidth, screenHeight = pyautogui.size() # 點擊屏幕上的指定位置打開繪圖軟件 pyautogui.click(x, y) # 等待繪圖軟件加載完成 time.sleep(1) # 將鼠標移動到繪圖區(qū)域的左上角 pyautogui.moveTo(x, y) # 按下鼠標左鍵并拖動到繪圖區(qū)域的右上角 pyautogui.dragTo(x + width, y, duration=0.5, button='left') # 拖動到繪圖區(qū)域的右下角 pyautogui.dragTo(x + width, y + height, duration=0.5, button='left') # 拖動到繪圖區(qū)域的左下角 pyautogui.dragTo(x, y + height, duration=0.5, button='left') # 拖動回繪圖區(qū)域的左上角,形成一個閉合圖形 pyautogui.dragTo(x, y, duration=0.5, button='left')
我們運行試一下效果,實現(xiàn)鼠標定位和拖拽效果,具有條件的同學可以嘗試一下。
再寫一個鍵盤調(diào)用的程序:
import pyautogui import time # 打開記事本應用程序 pyautogui.hotkey('win', 'r') pyautogui.typewrite('notepad') pyautogui.press('enter') # 等待記事本應用程序打開 time.sleep(2) # 打出英文名言,記得將輸入法切換為英文。 pyautogui.typewrite('To be, or not to be: that is the question.\nAsk not what your country can do for you, \nask what you can do for your country.\nI am the master of my fate,\n I am the captain of my soul.\n') # 保存文件 pyautogui.hotkey('ctrl', 's') time.sleep(2) pyautogui.typewrite('test') pyautogui.press('enter') # 關閉記事本應用程序 pyautogui.hotkey('alt', 'f4')
方法補充
Python PyAutoGUI 控制鼠標、鍵盤和屏幕操作
1. 鼠標控制
import pyautogui # 獲取屏幕尺寸 screen_width, screen_height = pyautogui.size() # 絕對坐標移動 pyautogui.moveTo(500, 300, duration=0.5) # 移動到(500,300) # 相對移動 pyautogui.move(100, -50) # 右移100px, 上移50px # 點擊操作 pyautogui.click() # 左鍵單擊 pyautogui.rightClick(600, 400) # 右鍵點擊指定位置 pyautogui.doubleClick() # 左鍵雙擊 # 拖拽操作 pyautogui.dragTo(800, 600, button='left') # 拖拽到目標位置 pyautogui.drag(0, 200, duration=1) # 垂直向下拖拽
2. 鍵盤操作
# 文本輸入 pyautogui.write('Hello@世界!', interval=0.1) # 支持Unicode # 按鍵操作 pyautogui.press('enter') # 按回車鍵 pyautogui.press(['tab', 'space']) # 按多個鍵 # 組合快捷鍵 pyautogui.hotkey('ctrl', 'c') # 復制 pyautogui.hotkey('ctrl', 'shift', 'esc') # 打開任務管理器 # 長按操作 pyautogui.keyDown('shift') # 按住Shift pyautogui.press('4') # 輸入$符號 pyautogui.keyUp('shift') # 釋放Shift
3. 屏幕處理
# 全屏截圖 pyautogui.screenshot('fullscreen.png') # 區(qū)域截圖 region = (100, 100, 500, 300) # (x, y, width, height) pyautogui.screenshot('area.png', region=region) # 像素分析 color = pyautogui.pixel(500, 300) # 獲取(500,300)處RGB值 if color == (255, 0, 0): print("檢測到紅色像素") # 圖像識別(需OpenCV) try: # 定位圖像中心點 x, y = pyautogui.locateCenterOnScreen('button.png', confidence=0.9) pyautogui.click(x, y) except pyautogui.ImageNotFoundException: print("未找到目標圖像")
到此這篇關于Python實現(xiàn)自動化調(diào)用鼠標和鍵盤的文章就介紹到這了,更多相關Python調(diào)用鼠標和鍵盤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python程序打包工具py2exe和PyInstaller詳解
這篇文章主要介紹了Python程序打包工具py2exe和PyInstaller詳解,如果可以提前將程序打包成 Windows平臺的 .exe 文件或者是Linux下的 .sh 腳本,那么使用起來就會方便很多,需要的朋友可以參考下2019-06-06