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 packagepyautoguito achieve the goal. To install the package, if you use windows system, you just need to run:
if you useosxsystem, you need to install three packages instead of pip install it directly:
The packagetreatthe 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.
Mouse
We mainly use three kinds of functions: click, move and drag.
Click
Click function simulates the mouse click.
We can control the position, left-button/right-button, times of click by parameters.
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 movetothe position (x,y).moveRellet the mouse move (x,y) pixelsrelativeto the current position ofmouse. Another parameter isduration, which controls the time of moving.
Drag
Drag function simulates the dragging ofmouse. Just like move function, there are two kinds:dragToanddragRel. One is to thepopsitionand the other is to drag relative to the current position.
Keyboard
We can simulate the pressing and typewriting onkeyboard.
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.
typewrite
We use typewrite function to press the letters and numbers. Also, we can use it to type long string.
Reference
[1] Introduction of PyAutoGUI : https://pyautogui.readthedocs.io/en/latest/introduction.html
[2] 使用 Python 实现鼠标键盘自动化 2 : https://python.freelycode.com/contribution/detail/579
领取专属 10元无门槛券
私享最新 技术干货