# Pyautogui - Python 操作鼠标键盘

## 安装

``````pip install python3–xlib
apt–get install scrot
apt–get install python3–tk
apt–get install python3–dev
pip install pyautogui
``````

## 鼠标操作

### 鼠标移动

pyautogui 使用 x-y 坐标系，左上角的坐标是`(0, 0)`

#### 获取屏幕分辨率

``````screen_width, screen_height = pyautogui.size()
``````

#### 获取当前鼠标坐标

``````x, y = pyautogui.position()
``````

#### 使用绝对坐标移动(`moveTo`)

``````for i in range(10):
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.moveTo(400, 300, duration=0.25)
pyautogui.moveTo(400, 400, duration=0.25)
pyautogui.moveTo(300, 400, duration=0.25)
``````

``````screen_width, screen_height = pyautogui.size()
print('screen_width: %s\nscreen_height: %s' % (screen_width, screen_height))

r = 250 # 圆半径
o_x = screen_width / 2 # 圆心X坐标
o_y = screen_height / 2 # 圆心Y坐标
pi = math.pi

for i in range(10):
for angle in range(0, 360, 20):
# 利用圆的参数方程
x = o_x + r * math.sin(angle * pi / 180)
y = o_y + r * math.cos(angle * pi / 180)
pyautogui.moveTo(x, y, duration=0.1)
``````

#### 使用相对坐标移动(`moveRel`)

``````for i in range(10):
pyautogui.moveRel(100, 0, duration=0.25)
pyautogui.moveRel(0, 100, duration=0.25)
pyautogui.moveRel(-100, 0, duration=0.25)
pyautogui.moveRel(0, -100, duration=0.25)
``````

``````try:
while True:
x, y = pyautogui.position()
print(x, y)
except KeyboardInterrupt:
print('\nExit.')
``````

### 鼠标点击

``````pyautogui.click(x=cur_x, y=cur_y, button='left')
``````
• `x`, `y`是要点击的位置，默认是鼠标当前位置
• button 是要点击的按键，有三个可选值： `left`, `middle`, `right`，默认是`left`

``````pyautogui.click(button='right')
``````

``````pyautogui.click(100, 100)
``````

• `pyautogui.doubleClick()`: 双击
• `pyautogui.rightClick()`: 右击
• `pyautogui.middleClick()`: 中击

### 鼠标拖拽

#### 使用绝对坐标拖拽(`dragTo`)

``````# 画一个回字
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.dragTo(400, 300, duration=0.25)
pyautogui.dragTo(400, 400, duration=0.25)
pyautogui.dragTo(300, 400, duration=0.25)
pyautogui.dragTo(300, 300, duration=0.25)

pyautogui.moveTo(200, 200, duration=0.25)
pyautogui.dragTo(500, 200, duration=0.25)
pyautogui.dragTo(500, 500, duration=0.25)
pyautogui.dragTo(200, 500, duration=0.25)
pyautogui.dragTo(200, 200, duration=0.25)
``````

#### 使用相对坐标拖拽(`dragRel`)

``````# 画一个回字
pyautogui.moveTo(300, 300, duration=0.25)
pyautogui.dragRel(100, 0, duration=0.25)
pyautogui.dragRel(0, 100, duration=0.25)
pyautogui.dragRel(-100, 0, duration=0.25)
pyautogui.dragRel(0, -100, duration=0.25)

pyautogui.moveTo(200, 200, duration=0.25)
pyautogui.dragRel(300, 0, duration=0.25)
pyautogui.dragRel(0, 300, duration=0.25)
pyautogui.dragRel(-300, 0, duration=0.25)
pyautogui.dragRel(0, -300, duration=0.25)
``````

### 滚轮

``````pyautogui.scroll(200)
``````

### 根据像素颜色定位按钮位置

``````img = pyautogui.screenshot()  # 截屏
img_color = img.getpixel((300, 300))  # 获取坐标颜色 (48, 10, 36)
is_matched = pyautogui.pixelMatchesColor(300, 300, (48, 10,36))  # 判断屏幕坐标的像素颜色是不是等于某个值
print(is_matched)  # True
``````

### 根据按钮图片定位按钮位置

``````corn_locate = pyautogui.locateOnScreen('corn/settings.png') # 找到按钮所在坐标，分别含义是按钮左上角x坐标，左上角y坐标，x方向大小，y方向大小 (5, 560, 54, 54)
corn_center_x, corn_center_y = pyautogui.center(corn_locate) # 找到按钮中心点
pyautogui.click(corn_center_x, corn_center_y) # 点击按钮
``````

## 键盘操作

### 键盘按键

#### 输入普通字符串

``````pyautogui.typewrite('Hello, world!', 0.25)  # 0.25表示每输完一个字符串延时0.25秒
``````

#### 输入特殊字符

`enter`/`return`/`\n` 回车
`esc` ESC键
`shiftleft`/`shiftright` 左右SHIFT键
`altleft`/`altright` 左右ALT键
`ctrlleft`/`ctrlright` 左右CTRL键
`tab`/`\t` TAB键
`backspace`/`delete` BACKSPACE/DELETE键
`pageup`/`pagedown` PAGE UP/PAGE DOWN键
`home`/`end` `HOME`/`END`
`up`/`down`/`left`/`right` 上下左右键
`f1`/`f2`/... F1/F2/...
`volumemute`/`volumedown`/`volumeup` ？？
`pause` PAUSE键
`capslock`/`numlock`/`scrolllock` CAPS LOCK/NUM LOCL/SCROLL LOCK键
`insert` INSERT键
`printscreen` PRINT SCREEN键
`winleft`/`winright` 左右Win键
`command` Mac上的command键
``````pyautogui.click(100, 100)
pyautogui.typewrite('Hello, world!', 0.25)
pyautogui.typewrite(['enter', 'a', 'b', 'left', 'left', 'X', 'Y'], '0.1')
``````

#### 键盘的按下和释放

• `keyDown()`: 按下某个键
• `keyUp()`: 松开某个键
• `press()`: 一次完整的按键，前面两个函数的结合

``````pyautogui.keyDown('altleft')
pyautogui.press('f4')
pyautogui.keyUp('altleft')
``````

``````pyautogui.hotkey('altleft', 'f4')
``````