Behavior Simulation of mouse and keyboard in Python

We can use Python to control our mouse and keyboard and let them finish some tasks automatically, such as pressing and dragging.

Preparation

We use Python package pyautogui to achieve the goal.
To install the package,
if you use windows system, you just need to run:

pip install pyautogui

if you use osx system, you need to install three packages instead of pip install it directly:

pip install pyobjc-core
pip install pyobjc
pip install pyautogui

The package treat the computer screen by pixel. We use

pyautogui.size()

to get the size of our screen.
Sometimes to delay the movement of mouse and keyboard, we need package time and function sleep.

import time
time.sleep(2)  #wait for 2 seconds and then run the print command.
print("awake")

Mouse

We mainly use three kinds of functions: click, move and drag.

Click

Click function simulates the mouse click.

pyautogui.click()

We can control the position, left-button/right-button, times of click by parameters.

pyautogui.click(x=100,y=100,button='right',clicks=2)

It simulates the behavior that we right-click the mouse twice at the location (100,100) on our screen.

Move

Move function simulates the mouse moving.
There are mainly two kinds of function: moveTo and moveRel.
MoveTo function let the mouse move to the position (x,y). moveRel let the mouse move (x,y) pixels relative to the current position of mouse.
Another parameter is duration, which controls the time of moving.

pyautogui.moveTo(100,100,duration=1)
pyautogui.moveRel(100,100,duration=1)

Drag

Drag function simulates the dragging of mouse.
Just like move function, there are two kinds: dragTo and dragRel. One is to the popsition and the other is to drag relative to the current position.

pyautogui.dragTo(100,100,duration=1)
pyautogui.dragRel(100,100,duration=1)

Keyboard

We can simulate the pressing and typewriting on keyboard.

Press

We use press function mainly to press the keys which are not letters and numbers(such as 'enter', 'shift'). We can also press several keys at the same time.

pyautogui.press('enter','shift')

typewrite

We use typewrite function to press the letters and numbers. Also, we can use it to type long string.

pyautogui.press('h')
pyautogui.press('hello world!')

Reference

[1] Introduction of PyAutoGUI
[2] 使用 Python 实现鼠标键盘自动化 2

Comments
Write a Comment