文章背景:PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,利用它可以实现自动化任务。pyautogui模块中包含了一些函数,可以模拟鼠标移动、按键和滚动鼠标滚轮。本文对鼠标控制的相关函数进行介绍。

1 确定鼠标位置

1.1 坐标轴系统

pyautogui的鼠标函数使用x,y坐标,原点在屏幕左上角,向右x坐标增加,向下y坐标增加,所有坐标都是正整数,没有负数坐标。

>>> import pyautogui>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.>>> print(screenWidth, screenHeight)1366 768

使用pyautogui.size()函数,获得屏幕的分辨率。根据屏幕分辨率的不同,返回值可能不同。

1.2 确定鼠标位置

>>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.>>> print(currentMouseX, currentMouseY)350 465

使用pyautogui.position()函数,确定鼠标当前的位置。

2 控制鼠标移动

pyautogui.moveTo(x,y[,duration = t]) 将鼠标移动到屏幕的指定位置

pyautogui.moveRel(x,y[,duration = t]) 相对于当前位置,移动鼠标。

duration为可选值,指定将鼠标移动到目标位置所需的秒数。

3 控制鼠标交互

3.1 点击鼠标

pyautogui.mouseDown()   #按下鼠标按键(左键)pyautogui.mouseUp()     #释放鼠标按键(左键)pyautogui.click()       #在当前光标位置,使用鼠标左键点击pyautogui.click([x,y,button='left/right/middle'])  #在(x,y)处点击鼠标左键/右键/中键                                                   #但不推荐使用这种方法,下面这种方法效果更好                                                   #pyautogui.moveTo(x,y,duration=t)                                                   #pyautogui.click()pyautogui.doubleClick() #双击鼠标左键pyautogui.rightClick()  #单击鼠标右键pyautogui.middleClick() #单击鼠标中键

3.2 拖动鼠标

pyautogui.dragTo(x,y[,duration=t])      #将鼠标拖动到指定位置pyautogui.dragRel(x,y[,duration=t])    #将鼠标拖动到相对当前位置的位置                                                               #x,y:水平移动,垂直移动

3.3 滚动鼠标

pyautogui.scroll(x)        #控制窗口上下滚动(滚动发生在鼠标的当前位置)                                       #正数表示向上滚动,负数表示向下滚动,

x代表一个整型参数,说明向上或向下滚动多少单位。单位的意义在每个操作系统和应用上不一样,需要自行尝试。

4 函数汇总

pyautogui.size()          # Get the size of the primary monitor.pyautogui.position()      # Get the XY position of the mouse.moveTo(x, y)              # Moves the mouse cursor to the given x and y coordinates.moveRel(xOffset, yOffset) # Moves the mouse cursor relative to its current position.mouseDown(x, y, button)   # Simulates pressing down the given button at the position x, y.mouseUp(x, y, button)     # Simulates releasing the given button at the position x, y.click(x, y, button)       # Simulates a click (left button by default).doubleClick()             # Simulates a double left-button click.rightClick()              # Simulates a right-button click.middleClick()             # Simulates a middle-button click.dragTo(x, y)              # Moves the mouse cursor while the left button is held down.dragRel(xOffset, yOffset) # Moves the mouse cursor relative to its current position while the left button is held down.scroll(units)             # Simulates the scroll wheel. A positive argument scrolls up; a negative argument scrolls down.

5 应用示例

5.1 现在鼠标在哪里?

在鼠标移动时,随时显示x, y坐标。

# from the book: Automate the Boring Stuff with Pythonprint('Press Ctrl-C to quit.')try:    while True:        # Get and print the mouse coordinates.        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('\nDone.')

5.2 绘制正方形旋转图案

在window10的画图软件中,选中铅笔,拖动鼠标,绘制一个正方形旋转图案。

# Adapted from the book: Automate the Boring Stuff with Pythonimport pyautogui, timetime.sleep(5)pyautogui.click()distance = 100while distance >0:    pyautogui.dragRel(distance,0,duration=0.2)  #move right    distance = distance - 5    pyautogui.dragRel(0,distance,duration=0.2)  #move down    pyautogui.dragRel(-distance,0,duration=0.2) #move left    distance = distance - 5    pyautogui.dragRel(0,-distance,duration=0.2) #move up    print("Done!")

参考资料:

  1. PyAutoGUI使用(https://ddz.red/wROQJ)

  2. PyAutoGUI——图形用户界面自动化(https://zhuanlan.zhihu.com/p/41662890)

  3. Automate the Boring Stuff with Python(https://ddz.red/y9qF5)

  4. Python编程快速上手—让繁琐工作自动化(https://ddz.red/AFTmO)

js鼠标移动到指定位置_Python: pyautogui模块之鼠标控制相关推荐

  1. 【原生JS】写滚动指定位置展示动画

    用原生js做滚动到指定位置展示动画,动画效果用Animate.css 直接上代码 <!DOCTYPE html> <html lang="en"><h ...

  2. python自动控制库_python PyAUtoGUI库实现自动化控制鼠标键盘

    PyAutoGUI 不知道你有没有用过,它是一款用Python自动化控制键盘.鼠标的库.但凡是你不想手动重复操作的工作都可以用这个库来解决. 比如,我想半夜时候定时给发个微信,或者每天自动刷页面等操作 ...

  3. js 跳转到指定位置 高德地图_【高德字符串JavaScript面试题】面试问题:高德地图Js … - 看准网...

    申请JSAPI的开发者key 申请地址:http://lbs.amap.com/dev/key 引入高德地图JavaScript API文件: 创建地图容器 在页面body里你想展示地图的地方创建一个 ...

  4. python怎么控制键盘和鼠标_Python pyautogui 控制键盘和鼠标

    1. 启动自动防故障功能 # 设置调用函数后停顿2s;启动自动防故障功能(将鼠标移到左上raise FailSafeException) pyautogui.PAUSE=2 pyautogui.FAI ...

  5. js在数组的指定位置添加元素

    let list = ['a', 'b', 'd'];// 第一个参数指定位置,第二个参数指定要删除的元素,如果为0,则追加 list.splice(2, 0, 'c');console.log(li ...

  6. 用python的pyautogui模块模拟鼠标点击和键盘输入

    import pyautogui import numpy as np import time#屏幕坐标与定位 width, height = pyautogui.size()#获取屏幕宽高 loca ...

  7. wxpython frame鼠标拖动_Python wxpython模块响应鼠标拖动事件操作示例

    本文实例讲述了Python wxpython模块响应鼠标拖动事件操作.分享给大家供大家参考,具体如下: wxpython鼠标拖动事件小案例: #coding:UTF-8 import wx app = ...

  8. python鼠标拖拽功能_Python wxpython模块响应鼠标拖动事件操作示例

    本文实例讲述了Python wxpython模块响应鼠标拖动事件操作.分享给大家供大家参考,具体如下: wxpython鼠标拖动事件小案例: #coding:UTF-8 import wx app = ...

  9. js 跳转到指定位置 高德地图_JS引入高德地图定位

    在此记录一下X项目使用高德地图的思路高德地图(X项目前端框架是Jquery) 2准备工作(封装方法,以便直接调用) -2.2封装超时方法($.timeOut-参考) -2.3封装异步调用地图的方法 / ...

最新文章

  1. jBPM3.12用户指南中文翻译----第一章 绪论
  2. python if调用函数,Python根据字符串调用函数过程解析
  3. 人脸识别python face_recognize_【python+face_recognition】人脸识别初始
  4. 在ArcGIS调坐标系引发的一系列问题
  5. 小程序 数据缓存
  6. 动态修改dom node的两种方法性能比较
  7. 默认构造函数的作用(“A”方法没有采用“0”个参数的重载
  8. C++智能指针shared_ptr使用实例
  9. PVID、Access、Trunk、Hybrid三种不同端口收发规则、Vlan中tagged端口和untagged端口的区别
  10. 计算机一级改扩展名,怎么改文件扩展名,教您电脑win7改文件扩展名的方法
  11. 马氏距离(Mahalanobis Distance)与欧式距离
  12. linux各种命令手册
  13. 刷题笔记——青蛙跳台阶问题汇总
  14. Python获取英雄联盟的皮肤原画:新手玩家们都懵了!(一)
  15. 判断一个序列是否为栈的有效输出序列
  16. Word文档段落的前后间距单位磅改为行,行改为磅方法演示
  17. 网格布局(grid布局)
  18. BackTrack安装
  19. RuntimeError: Physical devices cannot be modified after being initialized
  20. arduino播放小星星

热门文章

  1. 文献学习(part10)--元自步学习
  2. 文献学习(part7)--A strategy to incorporate prior knowledge into correlation network cutoff selection
  3. CSS基础(part6)--CSS的颜色表示
  4. 走近分形与混沌(part16)--三与自组织
  5. mysql定时木马_Mysql的语句生成后门木马的方法
  6. SAP UI5 应用开发教程之三十二 - 如何创建一个自定义 SAP UI5 控件试读版
  7. SAP Spartacus footer区域的一些常见错误
  8. SAP Cloud for Customer(C4C)后台ABAP系统的System ID和client ID
  9. SAP Spartacus 的 cx-page-layout selector 介绍 - PageLayoutComponent
  10. Chrome浏览器里的-webkit-focus-ring-color