Table of contents
Get the position of the mouse on the screen
Click on the dialog box to enter text
Precisely control the keyboard (method of inputting Chinese)
pyautogui installation
pip3 install pyautogui
Common operations
import pyautogui
screenWidth, screenHeight = pyautogui.size() # 屏幕尺寸
mouseX, mouseY = pyautogui.position() # 返回当前鼠标位置,注意坐标系统中左上方是(0, 0)
#为了保持用户可以随时干预鼠标键盘的动作,比较好的方法是添加停顿,或者强制结束,否则想停了鼠标还一直在晃就会失控,下面是两种对应的设置
pyautogui.PAUSE = 1.5 # 每个函数执行后停顿1.5秒
pyautogui.FAILSAFE = True # 鼠标移到左上角会触发FailSafeException,因此快速移动鼠标到左上角也可以停止
# 判断(x,y)是否在屏幕上
x, y = 122, 244
pyautogui.onScreen(x, y) # 结果为true
mouse operation
move
w, h = pyautogui.size()
pyautogui.moveTo(w/2, h/2) # 基本移动
pyautogui.moveTo(100, 200, duration=2) # 移动过程持续2s完成
pyautogui.moveTo(None, 500) # X方向不变,Y方向移动到500
pyautogui.moveRel(-40, 500) # 相对位置移动
pyautogui.moveRel(50, 0, duration=0.25) # 从当前位置右移100像素
pyautogui.moveRel(0, 50, duration=0.25) # 向下
pyautogui.moveRel(-50, 0, duration=0.25) # 向左
pyautogui.moveRel(0, -50, duration=0.25) # 向上