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.


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


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.


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


Click function simulates the mouse click.

We can control the position, left-button/right-button, times of click by parameters.,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 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.



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.



We can simulate the pressing and typewriting on keyboard.


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.'enter','shift')


We use typewrite function to press the letters and numbers. Also, we can use it to type long string.'h')'hello world!')


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

Write a Comment