pyautogui 文檔(二):鼠標控制


0,0       X increases -->
+---------------------------+
|                           | Y increases
|                           |     |
|   1920 x 1080 screen      |     |
|                           |     V
|                           |
|                           |
+---------------------------+ 1919, 1079

屏幕分辨率大小由size()函數返回為兩個整數的元組。position()函數返回鼠標光標的當前X和Y坐標

>>> pyautogui.size()
(1920, 1080)
>>> pyautogui.position()
(187, 567)

這是一個簡短的Python 3程序,它將不斷打印出鼠標光標的位置:

#! python3
import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
    print('\n')

onScreen()函數:檢查屏幕上是否有XY坐標

>>> pyautogui.onScreen(0, 0)
True
>>> pyautogui.onScreen(0, -1)
False
>>> pyautogui.onScreen(0, 99999999)
False

moveTo():將鼠標光標多少秒內移動到到您傳遞的X和Y整數坐標

>>> pyautogui.moveTo(100, 200,1)   # moves mouse to X of 100, Y of 200.
>>> pyautogui.moveTo(None, 500,2)  # moves mouse to X of 100, Y of 500.
>>> pyautogui.moveTo(600, None,3)  # moves mouse to X of 600, Y of 500.

(如果持續時間小於pyautogui.MINIMUM_DURATION移動將是即時的。默認情況下,pyautogui.MINIMUM_DURATION為0.1。)

move():將鼠標光標相對於其當前位置移動幾個像素

dragTo()drag():點擊鼠標按住拖動至指定位置

>>> pyautogui.dragTo(100, 200, button='left')     # drag mouse to X of 100, Y of 200 while holding down left mouse button
>>> pyautogui.dragTo(300, 400, 2, button='left')  # drag mouse to X of 300, Y of 400 over 2 seconds while holding down left mouse button
>>> pyautogui.drag(30, 0, 2, button='right')   # drag the mouse left 30 pixels over 2 seconds while holding down the right mouse button

click():模擬鼠標當前位置的單個鼠標左鍵單擊。“點擊”定義為按下按鈕然后將其釋放。

>>> pyautogui.click()  # click the mouse

moveTo()在單擊之前組合調用,請傳遞xy關鍵字參數的整數

>>> pyautogui.click(x=100, y=200,button='right',clicks=3, interval=0.25) # 移動至(100,200)右擊3次,每次講0.25秒

doubleClick():雙擊鼠標左鍵

mouseDown():按下鼠標

mouseUp():松開鼠標

pyautogui.doubleClick(x,y,interval,button)
pyautogui.mouseDown(button='right')
pyautogui.mouseUp(button='right', x=100, y=200)

鼠標滾動

可以通過調用scroll()函數並傳遞整數個“點擊”來滾動來模擬鼠標滾輪“點擊”中的滾動量因平台而異。(可選)可以傳遞整數xy關鍵字參數,以便在執行滾動之前移動鼠標光標。例如:

>>> pyautogui.scroll(10)   # scroll up 10 "clicks"
>>> pyautogui.scroll(-10)  # scroll down 10 "clicks"
>>> pyautogui.scroll(10, x=100, y=100)  # move mouse cursor to 100, 200, then scroll up 10 "clicks"

 

在OS X和Linux平台上,PyAutoGUI還可以通過調用hscroll()函數來執行水平滾動。例如:

>>> pyautogui.hscroll(10)   # scroll right 10 "clicks"
>>> pyautogui.hscroll(-10)   # scroll left 10 "clicks"

 

scroll()函數是一個包裝器vscroll(),用於執行垂直滾動。

 

參考:https://pyautogui.readthedocs.io/en/latest/mouse.html#mouse-scrolling


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM