鼠标控制函数¶
屏幕与鼠标位置¶
屏幕上的位置通过X和Y笛卡尔坐标表示。X坐标从左侧的0开始,向右递增。与数学坐标系不同,Y坐标从顶部的0开始,向下递增。
0,0 X increases -->
+---------------------------+
| | Y increases
| | |
| 1920 x 1080 screen | |
| | V
| |
| |
+---------------------------+ 1919, 1079
屏幕左上角的像素坐标为0, 0。如果您的屏幕分辨率为1920 x 1080,那么右下角的像素坐标将是1919, 1079(因为坐标从0开始计数,而非1)。
屏幕分辨率尺寸由size()函数返回,为一个包含两个整数的元组。鼠标光标当前的X和Y坐标由position()函数返回。
例如:
>>> 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')
这是Python 2版本:
#! python
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,
print '\b' * (len(positionStr) + 2),
sys.stdout.flush()
except KeyboardInterrupt:
print '\n'
要检查XY坐标是否在屏幕上,请将它们(作为两个整数参数或包含两个整数的单个元组/列表参数)传递给onScreen()函数,如果坐标在屏幕边界内则返回True,否则返回False。例如:
>>> pyautogui.onScreen(0, 0)
True
>>> pyautogui.onScreen(0, -1)
False
>>> pyautogui.onScreen(0, 99999999)
False
>>> pyautogui.size()
(1920, 1080)
>>> pyautogui.onScreen(1920, 1080)
False
>>> pyautogui.onScreen(1919, 1079)
True
鼠标移动¶
moveTo()函数会将鼠标光标移动到您传入的X和Y整数坐标位置。对于某个坐标,可以传入None值表示"当前鼠标光标位置"。例如:
>>> pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
>>> pyautogui.moveTo(None, 500) # moves mouse to X of 100, Y of 500.
>>> pyautogui.moveTo(600, None) # moves mouse to X of 600, Y of 500.
通常鼠标光标会立即移动到新坐标。如果您希望鼠标逐渐移动到新位置,可以传入第三个参数来指定移动过程应持续的时长(以秒为单位)。例如:
>>> pyautogui.moveTo(100, 200, 2) # moves mouse to X of 100, Y of 200 over 2 seconds
(如果持续时间小于pyautogui.MINIMUM_DURATION,移动将立即完成。默认情况下,pyautogui.MINIMUM_DURATION为0.1。)
如果你想将鼠标光标从当前位置相对移动几个像素,可以使用move()函数。该函数的参数与moveTo()类似。例如:
>>> pyautogui.moveTo(100, 200) # moves mouse to X of 100, Y of 200.
>>> pyautogui.move(0, 50) # move the mouse down 50 pixels.
>>> pyautogui.move(-30, 0) # move the mouse left 30 pixels.
>>> pyautogui.move(-30, None) # move the mouse left 30 pixels.
鼠标拖拽操作¶
PyAutoGUI的dragTo()和drag()函数具有与moveTo()和move()函数相似的参数。此外,它们还有一个button关键字参数,可以设置为'left'、'middle'和'right'来指定拖动时按住哪个鼠标按键。例如:
>>> 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
缓动/过渡函数¶
缓动效果是为鼠标移动增添花哨效果的额外功能。如果您对此不感兴趣,可以跳过本节。
补间或缓动函数决定了鼠标移动到目标位置时的运动过程。通常情况下,当鼠标在一定时间内移动时,它会以恒定速度沿直线直接移向目标位置。这种运动方式被称为线性补间或线性缓动函数。
PyAutoGUI在pyautogui模块中还提供了其他缓动函数。可以将pyautogui.easeInQuad函数作为第四个参数传递给moveTo()、move()、dragTo()和drag()函数,使鼠标光标开始缓慢移动然后加速到达目标位置。总持续时间仍与传递给函数的参数相同。pyautogui.easeOutQuad则相反:鼠标光标开始快速移动但在接近目标时减速。pyautogui.easeOutElastic会超过目标位置并像橡皮筋一样来回弹跳,直到最终停在目标位置。
例如:
>>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad) # start slow, end fast
>>> pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad) # start fast, end slow
>>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad) # start and end fast, slow in middle
>>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce) # bounce at the end
>>> pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic) # rubber band at the end
这些缓动函数是从Al Sweigart的PyTweening模块复制而来:https://pypi.python.org/pypi/PyTweening https://github.com/asweigart/pytweening 使用这些缓动函数时无需安装该模块。
如果你想创建自己的缓动函数,需要定义一个接收单个浮点参数的函数,该参数取值范围在0.0(表示鼠标移动的起点)到1.0(表示鼠标移动的终点)之间,并返回一个介于0.0和1.0之间的浮点值。
鼠标点击¶
click() 函数模拟在当前鼠标位置进行一次左键单击操作。"单击"被定义为按下按钮然后释放的过程。例如:
>>> pyautogui.click() # click the mouse
要在点击前结合moveTo()调用,请为x和y关键字参数传入整数值:
>>> pyautogui.click(x=100, y=200) # move to 100, 200, then click the left mouse button.
要指定不同的鼠标按键进行点击,请为button关键字参数传入'left'(左键)、'middle'(中键)或'right'(右键):
>>> pyautogui.click(button='right') # right-click the mouse
要实现多次点击,可以向clicks关键字参数传入一个整数。此外,你还可以向interval关键字参数传入浮点数或整数来指定点击之间的间隔时间(单位:秒)。例如:
>>> pyautogui.click(clicks=2) # double-click the left mouse button
>>> pyautogui.click(clicks=2, interval=0.25) # double-click the left mouse button, but with a quarter second pause in between clicks
>>> pyautogui.click(button='right', clicks=3, interval=0.25) ## triple-click the right mouse button with a quarter second pause in between clicks
作为便捷操作,doubleClick()函数将执行鼠标左键的双击操作。它还包含可选参数x、y、interval和button。例如:
>>> pyautogui.doubleClick() # perform a left-button double click
还有一个具有类似可选关键字参数的tripleClick()函数。
rightClick() 函数包含可选的 x 和 y 关键字参数。
mouseDown() 和 mouseUp() 函数¶
鼠标点击和拖拽操作都是由按下鼠标按键和释放按键两个动作组成。如果您需要分别执行这些操作,可以调用mouseDown()和mouseUp()函数。它们具有相同的x、y和button参数。例如:
>>> pyautogui.mouseDown(); pyautogui.mouseUp() # does the same thing as a left-button mouse click
>>> pyautogui.mouseDown(button='right') # press the right button down
>>> pyautogui.mouseUp(button='right', x=100, y=200) # move the mouse to 100, 200, then release the right button up.
鼠标滚动¶
可以通过调用scroll()函数并传递一个整数"点击"值来模拟鼠标滚轮滚动。不同平台上"点击"对应的滚动量可能有所不同。可选地,可以为x和y关键字参数传递整数值,以便在执行滚动前移动鼠标光标。例如:
>>> 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()的封装,用于执行垂直滚动操作。