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

猜你喜欢

转载自www.cnblogs.com/gexbooks/p/10789565.html