Beat boring office repetitions and use Python to control mouse and keyboard

Time:2020-6-29

Are you still bothered by the need to repeatedly click or submit forms on your computer?

If I told you that I could write the relevant operation instructions in advance through python, and let it help you operate the mouse and keyboard, and you are talking with your sister, isn’t it beautiful?

1、 Pyauogui Library

We can first install the library pyauogui, through which you can write some Python scripts to control your mouse and keyboard. For example, you can define where the mouse is to click, and when the keyboard will input what content, so as to achieve automatic operation of all software applications you want to operateIt can run on win, MacOS, Linux platforms 。

You can install it via PIP:

python3 -m pip install pyautogui

Source code: https://github.com/asweigart/pyautogui

PIP home page: https://pypi.org/project/PyAutoGUI/

2、 Operating the mouse

After installation, you can use it. For example, if you want to know the location of the mouse, you can get it through the position method:

You can also write a method to get the location of the mouse in real time:

Continuously monitor the current position of the mouse through the while loop. This is how it works:

The click method to call the mouse can be as follows:

single click:

>>> pyautogui . click()

Right click:

>>> pyautogui . click(button = ‘right’ )

Double click:

>>> pyautogui . doubleClick()

For example, there is a handsome picture on my desktop:

If you want to use Python to operate the mouse to open it, you can gently place the mouse on the Shuai photo, and then get the coordinates of the mouse in this position:

It can be found that the location of this photo is in (74197), then you can call the double-click method of the mouse:

We pass in the coordinate position in the doubleclick method, so it can double-click where the mouse moves.

Of course, you can also call the moveto method to move the mouse. For example, you can move the mouse to the position of 100200

pg.moveTo(100,200,2)

Here, 2 means the number of seconds to move. The default value is 0.1, which is fast moving.

To drag the mouse, you can call the dragto method, such as:

pg.dragTo(300, 400, 2, button=’left’)

It refers to dragging from the current position to the specified coordinate position, where 2 represents the moving time. Button can be used to set the left or right button.

3、 Operating keyboard

Then we operate the keyboard

You can use press to call keyboard operations, such as:

Press enter:

>>> pyautogui . press( ‘enter’ )

Press the left key:

>>> pyautogui . press( ‘left’ )

Press Ctrl:

>>> pyautogui . press( ‘ctrl’ )

Hotkey can be used for shortcut keys, such as:

Press Ctrl + Shift + T:

>>> pyautogui . hotkey( ‘ctrl’ , ‘shift’ , ‘t’ )

Copy:

>>> pyautogui . hotkey( ‘ctrl’ , ‘c’ )

Hold down to use the Keydown method, and release the key to use the Keyup method. For example:

Switch window:

This is how it works:

Just now pg.PAUSE=1 It refers to the execution of an operation instruction every other second.

To control the keyboard input, you can use the write method, such as:

Enter fxxkpython:

pg.write(‘fxxk Python !’, interval=0.3)

4、 Case study

Knowing these, you can go to play some interesting things, give a few simple examples to give you happy.

For example, assign it toBrowser search content :

This is how it works:

Is it a bit selenium.

Another exampleOperate it to chat on wechat :

Run a wave:

Take another official example. You can let it goDrawing

This is how it works: