Python controls the mouse and keyboard-PyAutoGUI usage details

PyAutoGUI-automate all GUI

Installation command:

pip install pyautogui

1 Introduction

1.1 Purpose

PyAutoGUI is a pure Python GUI automation tool, its purpose is to automatically control the mouse and keyboard operations with programs, multi-platform support (Windows, OS X, Linux). You can use pip to install, source code on Github.

The following code moves the mouse to the center of the screen.

import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

PyAutoGUI can simulate mouse movement, clicking, dragging, keyboard key input, pressing and holding, and pressing and holding the hot keys of the mouse + keyboard at the same time. It can be said that the hand can move.

pyautogui basic operation example

import pyautogio
# 获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()

# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()

# 鼠标移动坐标为100, 100位置  绝对移动
pyautogui.moveTo(100, 100)

# 鼠标左击
pyautogui.click()

# 鼠标向下移动 相对移动
pyautogui.moveRel(None, 10)

# 鼠标双击
pyautogui.doubleClick()

# 用缓动/渐变函数让鼠标2秒后移动到(500, 500)位置
# use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)

# 在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25)

# 键盘点击esc
pyautogui.press('esc')

# 按住shift键
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])

# 放开shift键
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')

PyAutoGUI keyboard table:

'enter' (or'return' or'\n')

Carriage return

‘esc’

ESC

'shifttleft', 'shiftright'

Left and right SHIFT keys

‘altleft’, ‘altright’

Left and right ALT keys

‘ctrlleft’, ‘ctrlright’

Left and right CTRL keys

‘tab’ (‘\t’)

TAB键

‘backspace’, ‘delete’

BACKSPACE 、DELETE键

‘pageup’, ‘pagedown’

PAGE UP 和 PAGE DOWN键

‘home’, ‘end’

HOME and END keys

‘up’, ‘down’, ‘left’, ‘right’

Arrow keys

'f1', 'f2', 'f3' ...

F1…….F12 key

‘volumemute’, ‘volumedown’, ‘volumeup’

Some keyboards do not

‘pause’

PAUSE key

'capslock', 'numlock', 'scrolllock'

CAPS LOCK, NUM LOCK, and SCROLL LOCK keys

‘insert’

INS or INSERT key

‘printscreen’

PRTSC or PRINT SCREEN key

‘winleft’, ‘winright’

Win key

‘command’

Mac OS X command key

Documentation:

https://muxuezi.github.io/posts/doc-pyautogui.html

http://pyautogui.readthedocs.io/en/latest/introduction.html

http://blog.csdn.net/ibiao/article/details/54406803

http://www.chenxm.cc/post/633.html

 

Guess you like

Origin blog.csdn.net/zhangge3663/article/details/108578809