1. 程式人生 > >pyautogui自動化控制滑鼠和鍵盤操作

pyautogui自動化控制滑鼠和鍵盤操作

pyautogui自動化控制滑鼠和鍵盤操作


PyAutoGUI是一個純Python的GUI自動化工具,其目的是可以用程式自動控制滑鼠和鍵盤操作,多平臺支援(Windows,OS X,Linux)。

安裝

pip3 install
pyautogui

pyautogui滑鼠操作樣例

import pyautogui

# 獲取當前螢幕解析度
screenWidth, screenHeight = pyautogui.size()

# 獲取當前滑鼠位置
currentMouseX, currentMouseY = pyautogui.position()

# 2秒鐘滑鼠移動座標為100,100位置  絕對移動
#pyautogui.moveTo(100, 100,2)
pyautogui.moveTo(x=100, y=100,duration=2, tween=pyautogui.linear)

#滑鼠移到螢幕中央。
pyautogui.
moveTo(screenWidth / 2, screenHeight / 2) # 滑鼠左擊一次 #pyautogui.click() # x # y # clicks 點選次數 # interval點選之間的間隔 # button 'left', 'middle', 'right' 對應滑鼠 左 中 右或者取值(1, 2, or 3) # tween 漸變函式 # pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear) # 滑鼠相對移動 ,向下移動
#pyautogui.moveRel(None, 10) pyautogui.moveRel(xOffset=None, yOffset=10,duration=0.0, tween=pyautogui.linear) # 滑鼠當前位置0間隔雙擊 #pyautogui.doubleClick() pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear) # 滑鼠當前位置3擊 #pyautogui.tripleClick() pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear) #右擊 pyautogui.rightClick() #中擊 pyautogui.middleClick() # 用緩動/漸變函式讓滑鼠2秒後移動到(500,500)位置 # use tweening/easing function to move mouse over 2 seconds. pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad) #滑鼠拖拽 pyautogui.dragTo(x=427, y=535, duration=3,button='left') #滑鼠相對拖拽 pyautogui.dragRel(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False) #滑鼠移動到x=1796, y=778位置按下 pyautogui.mouseDown(x=1796, y=778, button='left') #滑鼠移動到x=2745, y=778位置鬆開(與mouseDown組合使用選中) pyautogui.mouseUp(x=2745, y=778, button='left',duration=5) #滑鼠當前位置滾輪滾動 pyautogui.scroll() #滑鼠水平滾動(Linux) pyautogui.hscroll() #滑鼠左右滾動(Linux) pyautogui.vscroll()

pyautogui鍵盤操作樣例

#模擬輸入資訊
pyautogui.typewrite(message='Hello world!',interval=0.5)
#點選ESC
pyautogui.press('esc')
# 按住shift鍵
pyautogui.keyDown('shift')
# 放開shift鍵
pyautogui.keyUp('shift')
# 模擬組合熱鍵
pyautogui.hotkey('ctrl', 'c')

按鍵支援

按鍵 說明
enter(或return\n) 回車
esc ESC鍵
shiftleft, shiftright 左右SHIFT鍵
altleft, altright 左右ALT鍵
ctrlleft, ctrlright 左右CTRL鍵
tab (\t) TAB鍵
backspace, delete BACKSPACE 、DELETE鍵
pageup, pagedown PAGE UP 和 PAGE DOWN鍵
home, end HOME 和 END鍵
up, down, left,right 箭頭鍵
f1, f2, f3…. F1…….F12鍵
volumemute, volumedown,volumeup 有些鍵盤沒有
pause PAUSE鍵
capslock, numlock,scrolllock CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 鍵
insert INS或INSERT鍵
printscreen PRTSC 或 PRINT SCREEN鍵
winleft, winright Win鍵
command Mac OS X command鍵

提示資訊

alert

#pyautogui.alert('This is an alert box.','Test')
pyautogui.alert(text='This is an alert box.', title='Test')

在這裡插入圖片描述

option

#pyautogui.confirm('Shall I proceed?')
pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])

在這裡插入圖片描述

password

a = pyautogui.password('Enter password (text will be hidden)')
print(a)

在這裡插入圖片描述

prompt

a = pyautogui.prompt('input  message')
print(a)

在這裡插入圖片描述

截圖

整個螢幕截圖並儲存

im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')

im2 = pyautogui.screenshot('my_screenshot2.png')

螢幕查詢圖片位置並獲取中間點

#在當前螢幕中查詢指定圖片(圖片需要由系統截圖功能擷取的圖)
coords = pyautogui.locateOnScreen('folder.png')
#獲取定位到的圖中間點座標
x,y=pyautogui.center(coords)
#右擊該座標點
pyautogui.rightClick(x,y)

安全設定

import pyautogui

#保護措施,避免失控
pyautogui.FAILSAFE = True
#為所有的PyAutoGUI函式增加延遲。預設延遲時間是0.1秒。
pyautogui.PAUSE = 0.5