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

The pynput library allows you to control and monitor input devices such as the keyboard and mouse in Python. This powerful automation library enables you to move cursors, simulate clicks, and generate keystrokes programmatically.

In this tutorial, we'll learn how to automate mouse movements, clicks, and keyboard input using pynput's mouse and keyboard controllers.

Installation

Since pynput is not included with Python by default, install it using pip:

pip install pynput

Controlling the Mouse

Import the mouse controller and button constants from pynput:

from pynput.mouse import Button, Controller

# Create a mouse controller instance
mouse = Controller()

# Get current mouse position
print(f'Current position: {mouse.position}')

# Move mouse to specific coordinates
mouse.position = (100, 200)
print(f'New position: {mouse.position}')
Current position: (640, 360)
New position: (100, 200)

Mouse Movement and Clicks

You can move the mouse relative to its current position and simulate various click actions:

from pynput.mouse import Button, Controller

mouse = Controller()

# Move relative to current position
mouse.move(50, -30)

# Single left click
mouse.press(Button.left)
mouse.release(Button.left)

# Right click
mouse.click(Button.right, 1)

# Double click
mouse.click(Button.left, 2)

# Scroll (dx, dy) - positive dy scrolls up
mouse.scroll(0, 3)
print("Mouse actions completed!")
Mouse actions completed!

Controlling the Keyboard

Import the keyboard controller and key constants:

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Type individual characters
keyboard.press('h')
keyboard.release('h')
keyboard.press('i')
keyboard.release('i')

# Type complete strings
keyboard.type(' Hello World!')

# Use special keys
keyboard.press(Key.space)
keyboard.release(Key.space)
keyboard.press(Key.enter)
keyboard.release(Key.enter)

print("Keyboard simulation completed!")
Keyboard simulation completed!

Special Key Combinations

You can simulate key combinations like Ctrl+C, Alt+Tab, etc.:

from pynput.keyboard import Key, Controller
import time

keyboard = Controller()

# Simulate Ctrl+A (Select All)
keyboard.press(Key.ctrl)
keyboard.press('a')
keyboard.release('a')
keyboard.release(Key.ctrl)

time.sleep(1)

# Simulate Ctrl+C (Copy)  
keyboard.press(Key.ctrl)
keyboard.press('c')
keyboard.release('c')
keyboard.release(Key.ctrl)

print("Key combination executed!")
Key combination executed!

Complete Example: Automated Text Entry

Here's a practical example that combines mouse and keyboard automation:

from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController
import time

# Initialize controllers
mouse = MouseController()
keyboard = KeyboardController()

def automate_text_entry():
    # Move to text field position (adjust coordinates as needed)
    mouse.position = (400, 300)
    time.sleep(0.5)
    
    # Click to focus the text field
    mouse.click(Button.left, 1)
    time.sleep(0.5)
    
    # Type some text
    keyboard.type("This text was typed automatically!")
    time.sleep(0.5)
    
    # Press Enter
    keyboard.press(Key.enter)
    keyboard.release(Key.enter)
    
    print("Automation completed!")

# Uncomment to run (be careful with automation!)
# automate_text_entry()

Common Use Cases

Application Mouse Functions Keyboard Functions
GUI Testing Click buttons, navigate menus Fill forms, shortcuts
Game Automation Auto-clicking, precise movements Hotkeys, chat messages
Data Entry Navigate between fields Type repetitive text

Important Notes

Security and Ethics: Always use automation responsibly. Some applications may detect and block automated input. Ensure you have permission to automate interactions with software.

Cross-Platform: Pynput works on Windows, macOS, and Linux, but coordinate systems may vary between platforms.

Conclusion

The pynput library provides powerful tools for automating mouse and keyboard interactions in Python. Use it responsibly for legitimate automation tasks like testing, data entry, or accessibility tools.

---
Updated on: 2026-03-25T15:59:29+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements