安裝
pip3 install pyautogui
原文:https://blog.csdn.net/weixin_43430036/article/details/84650938
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')
pyautogui.screenshot(region=[x,y,w,h])
屏幕查找圖片位置並獲取中間點
#在當前屏幕中查找指定圖片(圖片需要由系統截圖功能截取的圖) 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
中文輸出
當你使用 pyautogui.write() 里面是中文,是無法輸出的。
這時你需要導入一個庫:pyperclip 。
這個庫中有一個方法 copy 也就是復制,比如我想復制中文。
import pyperclip content = "你好,世界!" pyperclip.copy(content)
再利用,pyautogui 的組合鍵,也就是 ctrl + v 進行粘貼。
遠程桌面失效
遠程桌面關閉后模擬鼠標鍵盤失效的問題:
原因分析
因為通過mstsc啟動遠程桌面連接時,被連接的Windows會啟動一個會話(Session)。此時你對遠程桌面窗口里面的所有操作(鼠標,鍵盤)將會‘翻譯’成TCP包傳輸過去,被連接的Windows接收到這些包之后,‘還原’命令並且在當前的活動會話上面執行。而當你斷開連接時(點X關閉),會話變成斷開狀態,Windows會自動關閉會話(事實上會話還是在的,只是狀態變成斷開的),也就導致了所有基於GUI的操作‘失效’了。
解決方案的腳本命令
@%windir%\System32\tscon.exe 0 /dest:console @%windir%\System32\tscon.exe 1 /dest:console @%windir%\System32\tscon.exe 2 /dest:console
把上面的這個腳本命令保存為.bat
批處理文件,放到桌面上,每次想斷開遠程連接的時候,點擊運行即可。