Fundamentals 5 min read

Using PyUserInput to Simulate Mouse and Keyboard Operations in Python

This article introduces the PyUserInput library, explains how to install platform‑specific dependencies, demonstrates basic mouse and keyboard control with code examples, and shows a practical script that automates a login process by moving the cursor, typing credentials, and clicking buttons.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using PyUserInput to Simulate Mouse and Keyboard Operations in Python

PyUserInput is a lightweight Python library that combines PyMouse and PyKeyboard to simulate mouse and keyboard actions, useful for automating repetitive tasks or creating simple game scripts.

Installation – Different operating systems require different dependencies: Linux needs Xlib, macOS needs Quartz and AppKit, and Windows needs pywin32 (pre‑installed) plus pyHook. After downloading the appropriate pyHook‑xxx.whl file, install it with pip install xxxxxx.whl , then install the library itself with pip install PyUserInput .

Basic Usage – Import the modules and create objects:

<code>from pymouse import PyMouse
from pykeyboard import PyKeyboard

m = PyMouse()
k = PyKeyboard()</code>

Mouse control – Get screen size, move the cursor, and click:

<code>x_dim, y_dim = m.screen_size()  # get screen size
m.move(100, 100)                # move to (100,100)
m.click(100, 100, 1, 1)         # left‑click once at (100,100)</code>

Keyboard control – Type strings and press/release keys:

<code>k.type_string("Laofei NB!")   # type a string
k.press_key("S")               # press S
k.release_key("S")             # release S
k.tap_key("S")                 # tap S</code>

Combination keys work as well; for example, copying with Ctrl+C can be done by pressing the control key, tapping "c", and releasing the control key:

<code>k.press_key(k.control_key)
k.tap_key('c')
k.release_key(k.control_key)</code>

Login Simulation Example – The script moves the mouse to the email field, clicks, types the address, moves to the password field, types the password, drags a slider, and finally clicks the login button:

<code># input email and password
m.move(590,370)
m.click(590,370,1,1)
k.type_string('[email protected]')
time.sleep(1)
m.move(590,440)
m.click(590,440,1,1)
k.type_string('memeda')
time.sleep(1)
# drag slider
m.move(590,510)
m.press(590,510,1)
time.sleep(1)
m.move(1000,500)
time.sleep(2)
# click login button
m.click(590,600)</code>

The script demonstrates how PyUserInput can replace Selenium for simple UI automation tasks.

PythonAutomationkeyboardmousePyUserInput
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.