博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pyautogui 文档(二):鼠标控制
阅读量:4708 次
发布时间:2019-06-10

本文共 2711 字,大约阅读时间需要 9 分钟。

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程序,它将不断打印出鼠标光标的位置:

#! python3import pyautogui, sysprint('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

转载于:https://www.cnblogs.com/gexbooks/p/10789565.html

你可能感兴趣的文章
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>