欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python 调用pyautogui 实时获取鼠标的位置、移动鼠标的方法

时间:2021-01-13 09:50:36|栏目:Python代码|点击:

PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,利用它可以实现自动化任务

本章介绍了许多不同函数,下面是快速的汇总参考:

moveTo(x,y)将鼠标移动到指定的 x、y 坐标。

moveRel (xOffset,yOffset)相对于当前位置移动鼠标。

dragTo(x,y)按下左键移动鼠标。

dragRel (xOffset,yOffset)按下左键,相对于当前位置移动鼠标。

click(x,y,button)模拟点击(默认是左键)。

rightClick() 模拟右键点击。

middleClick() 模拟中键点击。

doubleClick() 模拟左键双击。

mouseDown(x,y,button)模拟在 x、y 处按下指定鼠标按键。

mouseUp(x,y,button)模拟在 x、y 处释放指定键。

scroll (units)模拟滚动滚轮。正参数表示向上滚动,负参数表示向下滚动。

typewrite(message)键入给定消息字符串中的字符。

typewrite([key1,key2,key3])键入给定键字符串。

press(key)按下并释放给定键。

keyDown(key)模拟按下给定键。

keyUp(key)模拟释放给定键。

hotkey([key1,key2,key3])模拟按顺序按下给定键字符串,然后以相反的顺序释放。

screenshot() 返回屏幕快照的 Image 对象(参见第 17 章关于 Image 对象的信息)。

代码部分:

import os
import time
import pyautogui as pag
try:
 while True:
  print("Press Ctrl-C to end")
  screenWidth, screenHeight = pag.size() #获取屏幕的尺寸
  print(screenWidth,screenHeight)
  x,y = pag.position() #获取当前鼠标的位置
  posStr = "Position:" + str(x).rjust(4)+','+str(y).rjust(4)
  print(posStr)
  time.sleep(0.2)
  os.system('cls') #清楚屏幕
except KeyboardInterrupt:
 print('end....')

实际效果

import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
# 鼠标向下移动10像素
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
# use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(1800, 500, duration=2, tween=pyautogui.easeInOutQuad)
# 在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25) #输入文本
pyautogui.press('esc') #按下按键
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')

上一篇:Python嵌套列表转一维的方法(压平嵌套列表)

栏    目:Python代码

下一篇:Python设置默认编码为utf8的方法

本文标题:python 调用pyautogui 实时获取鼠标的位置、移动鼠标的方法

本文地址:http://www.codeinn.net/misctech/44446.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有