How to control your mouse and keyboard using the pynput library in Python

PythonServer Side ProgrammingProgramming

Introduction

The pynput library allows you to control and monitor/listen to your input devices such as they keyboard and mouse.

The pynput.mouse allows you control and monitor the mouse, while the pynput.keyboard allows you to control and monitor the keyboard.

In this article, we will be moving the cursor to a specific position, automate clicks, and simulate keystrokes from the keyboard.

Without further ado, let’s get started.

Getting Started

Since the pynput module does not come packaged with Python, you will have to manually download and install it using the pip package manager.

To do this, launch your terminal and use the command below.

pip install pynput

Once the library has been successfully downloaded and installed, you are all set to import its various module to your Python script.

Since we will be importing various modules for both the keyboard and mouse, we will discuss about the import statements later on.

Controlling the Mouse

In order to control and simulate your mouse using Python, you must import the mouse module from the pynput library. Since we will be simulating clicks and movement as well, we will import them along with the module.

from pynput.mouse import Button, Controller

Firstly, we will be using Controller() method to move the mouse around the screen.

mouse = Controller()

Now, if you want to move your mouse to any position on your screen, you just provide its coordinates.

mouse.position = (50,60)

If you want to see where your mouse moved, you can print it.

print('Current mouse position −> {0}'.format(mouse.position))

If you want to move the mouse relative to its current position, use the move function.

mouse.move(30,15)

If you want to simulate button presses,

mouse.press(Button.left)
mouse.release(Button.left)
mouse.press(Button.right)
mouse.release(Button.right)

For double clicks, use

mouse.click(Button.left, 2)

You can even simulate scrolls using pynput,

mouse.scroll(0,2)

This will scroll two steps down, the x coordinate is used to navigate the scroll from left to right and the y coordinate for top to bottom.

Example

from pynput.mouse import Button, Controller
mouse = Controller()
mouse.position = (50,60)
print('Current mouse position −> {0}'.format(mouse.position))
mouse.move(30,15)
mouse.press(Button.left)
mouse.release(Button.left)
mouse.press(Button.right)
mouse.release(Button.right)
mouse.click(Button.left, 2)
mouse.scroll(0,2)

Using above methods, you can simulate drawing shapes on paint using basic coordinate geometry.

Controlling the Keyboard

Firstly, we import the required modules and functions. In the keyboard module within the pynput library, we will be using the Key and Controller functions.

From pynput.keyboard import Key, Controller

We will be using the Controller method to control the Keyboard and simulate keystrokes.

keyboard = Controller()

Now, to simulate keystrokes, we have the press and release methods

keyboard.press('a')
keyboard.release('a')

This works for all alphabets, including upper case. For upper case, you just use “A” instead of “a”.

You can simulate other keypresses such as ctrl, alt, space etc as well.

keyboard.press(Key.space)
keyboard.release(Key.space)
keyboard.press(Key.ctrl)
keyboard.release(Key.ctrl)

If you want to simulate typing in sentences or words at once, you can use the type function.

keyboard.type('Hello World!!')

And that’s basically how you control or simulate a keyboard device on Python.

Example

from pynput.keyboard import Key, Controller
keyboard = Controller()
keyboard.press('a')
keyboard.release('a')
keyboard.press(Key.space)
keyboard.release(Key.space)
keyboard.press(Key.ctrl)
keyboard.release(Key.ctrl)
keyboard.type('Hello World!!')

Conclusion

You now know how to simulate keyboard and mouse input devices using Python’s pynput library.

Using this, you can build automated bots that perform clicking actions in clicker games, build a spam bot that sends out various different forms of messages at once. The applications are quite limitless as you can build any kind of automation tool using this method.

SpamBot mini project − https://github.com/SVijayB/Spam−botz

If you want to read more about pynput and explore its various other functions, you can check out its official documentation at −

https://pynput.readthedocs.io/en/latest/index.html.

raja
Published on 11-Feb-2021 06:33:24
Advertisements