How to Control a Mouse using Python

Want to control your mouse using Python?

You can definitely do so using the PyAutoGUI library.

You may install the PyAutoGUI library using the following command (under Windows):

pip install pyautogui

Next, you’ll see the following 5 scenarios that demonstrate how to control a mouse using Python by:

  1. Moving a mouse cursor to a specific position
  2. Clicking on a specific location
  3. Double-clicking a given location
  4. Moving a file to a folder
  5. Selecting a cell in Excel, and then dragging it down

5 Scenarios of Controlling a Mouse using Python

Scenario 1: Moving a mouse cursor to a specific position

In the first scenario, you’ll see how to move the mouse cursor to a specific location.

For example, let’s say that your goal is to move the mouse cursor to ‘This PC‘ on your desktop. You may then use the template below in order to move the cursor to your desired location (note that you’ll need to specify the X and Y coordinates. The PyAutoGUI Documentation contains a simple Python program that would print the X and Y coordinates. Alternatively, you may use trial and error):

import pyautogui
pyautogui.moveTo(X coordinate, Y coordinate)

In my case, the code to move the mouse cursor to ‘This PC’ is:

import pyautogui
pyautogui.moveTo(50, 50)

Once I ran the code, the cursor moved to ‘This PC’:

This PC

Scenario 2: Clicking on a specific location

What if you want to click on a specific location?

For instance, what if you want to single-click on ‘This PC‘?

In this case, simply apply pyautogui.click at your desired coordinates.

This is the code that I used:

import pyautogui
pyautogui.click(50, 50)

When I ran the code, I got a single-click on ‘This PC’:

Select This PC

Scenario 3: Double-clicking a given location

In some cases, you may need to open a file or directory. To accomplish this task, you’ll need to double-click your desired location.

To double-click your desired location, simply set clicks=2:

import pyautogui
pyautogui.click(50, 50, clicks=2)

This will open up ‘This PC’:

Open This PC

Scenario 4: Moving a file to a folder

Let’s now see how to move a file to a folder using PyAutoGUI.

For example, suppose that the goal is to move the ‘Products’ file to the ‘test_1’ folder:

How to Control a Mouse using Python

To accomplish this goal, you may use a combination of moveTo and dragTo (where the dragTo is set for 2 seconds):

import pyautogui
pyautogui.moveTo(1400, 445)
pyautogui.dragTo(1700, 445, 2)

The Products file will then get dragged to the test_1 folder over 2 seconds:

How to Control a Mouse using Python

The file will then appear inside the test_1 folder as follows:

Folder

Scenario 5: Selecting a cell in Excel, and then dragging it down

In this scenario, you’ll see how to select a cell in Excel and then drag it down.

For example, let’s say that you want to select cell A4 (which contains the value of ‘Laptop’), and then drag it all the way to cell A13 (and thus, populating the ‘Laptop’ value across the range of cells of A4 to A13):

Excel File

Note that you’ll need to position the mouse cursor on the bottom right corner of cell A4 before you can drag it to cell A13.

Here is the code that I applied:

import pyautogui

pyautogui.moveTo(100, 300,1)
pyautogui.dragTo(150, 305,1, button = 'left')
pyautogui.dragTo(150, 505,1)

As you can see, the ‘Laptop’ value got populated across the range of cells of A4 to A13:

How to Control a Mouse using Python

You may also want to check the following guide that explains how to control a keyboard using Python.