A Python wrapper for Linux /dev/uinput keystroke simulation
Project description
rs_uinput_kb
A high-performance Python module written in Rust (via PyO3) for simulating hardware-level keystrokes on Linux. By interfacing directly with /dev/uinput, this library creates a virtual USB keyboard, making it virtually undetectable by software-level input hooks.
It features built-in humanization (randomized typing delays) and a powerful macro syntax for executing complex keystroke combinations, modifier holds, and special characters all from a single string.
Features
- Hardware-Level Emulation: Bypasses X11/Wayland entirely. Types directly at the kernel level.
- Humanized Typing: Jittered delays between characters, randomized key-press dwell times, and varied shift-key hesitations.
- Inline Macro Syntax: Trigger special keys (
{enter},{backspace}) and modifier holds ({ctrl down}c{ctrl up}) directly within your text strings. - Blazing Fast: Core logic written in safe Rust.
Prerequisites
- Linux OS (Requires
/dev/uinputsupport) - Python 3.7+
- Rust & Cargo (for building)
- Root/Sudo Access (or configure
udevrules so your user can write to/dev/uinput)
Installation & Building
This project uses maturin to build the Rust code into a Python module.
- Install
maturinin your Python environment:
pip install maturin
- Build and install the module directly into your current virtual environment:
maturin develop --release
Usage Guide
Important: Because this script accesses /dev/uinput, you will likely need to run your Python script with sudo (e.g., sudo python main.py), unless you have set up specific user permissions for uinput.
Basic Humanized Typing
By default, the function adds slight, randomized delays to mimic a real human typing.
import rs_uinput_kb
# Types a simple string with natural human-like delays
rs_uinput_kb.type_text("Hello, world! I am typing like a human.")
Instant / Machine-Speed Typing
If you want the text to appear instantaneously, set all delay parameters to 0.
import rs_uinput_kb
rs_uinput_kb.type_text(
"This will appear instantly.",
min_char_delay=0.0,
max_char_delay=0.0,
min_dwell_time=0,
max_dwell_time=0,
min_shift_delay=0,
max_shift_delay=0,
startup_delay_ms=200 # Only wait 200ms for OS recognition
)
Advanced: Macros & Special Keys
The module supports an inline macro syntax using curly braces {}. This allows you to mix normal typing with special commands seamlessly.
import rs_uinput_kb
# 1. Tapping Special Keys
# Types "User", presses Tab, types "Password", presses Enter
rs_uinput_kb.type_text("User{tab}Password{enter}")
# 2. Deleting Text
# Types a typo, then uses backspace to correct it
rs_uinput_kb.type_text("The wrong word{backspace}{backspace}{backspace}{backspace}right word.")
# 3. Holding Modifiers (e.g., Copy and Paste)
# Highlights all text, copies it, moves right, and pastes it
rs_uinput_kb.type_text("{ctrl down}a{ctrl up}{ctrl down}c{ctrl up}{right}{ctrl down}v{ctrl up}")
# 4. Escaping Brackets
# If you need to actually type a curly bracket, use double brackets
rs_uinput_kb.type_text("def my_function() {{ return True; }}")
Supported Special Keys:
backspace, enter, tab, esc, space, up, down, left, right, shift, ctrl, alt, meta (or win, cmd), insert, delete, home, end, pageup, pagedown, capslock, f1 through f12.
API Reference
rs_uinput_kb.type_text(text, kwargs)
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
str |
Required | The string of characters and {macros} to type. |
min_char_delay |
float |
0.05 |
Minimum wait time (in seconds) between pressing subsequent keys. |
max_char_delay |
float |
0.1 |
Maximum wait time (in seconds) between pressing subsequent keys. |
min_dwell_time |
int |
20 |
Minimum time (in milliseconds) a key is physically held down before release. |
max_dwell_time |
int |
60 |
Maximum time (in milliseconds) a key is physically held down before release. |
min_shift_delay |
int |
10 |
Minimum hesitation (in ms) before and after pressing the shift key for capital letters. |
max_shift_delay |
int |
30 |
Maximum hesitation (in ms) before and after pressing the shift key for capital letters. |
startup_delay_ms |
int |
500 |
Time (in ms) the script waits after creating the virtual /dev/uinput device before it starts typing. Note: If this is too low, the OS might miss the first few keystrokes. |
Troubleshooting
RuntimeError: Permission denied (os error 13)Your user does not have permission to read/write/dev/uinput. Run your Python script withsudoor add audevrule for theuinputgroup.- The first few characters are missing
The Linux kernel takes a fraction of a second to recognize a newly plugged-in (or virtual) USB device. Increase the
startup_delay_ms(e.g., to800or1000).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rs_uinput_kb-2.1.1.tar.gz.
File metadata
- Download URL: rs_uinput_kb-2.1.1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
378bdd914976ed1005ddc59de91b0bdab9868ff735f793657e028481c7426f0e
|
|
| MD5 |
7cc51f2cf43470865f9f17bbdde0ec63
|
|
| BLAKE2b-256 |
c1e4fd8c1c349ddedb268e340a89099ab29854d6139bd041f00e7762ab41654b
|
File details
Details for the file rs_uinput_kb-2.1.1-cp311-cp311-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: rs_uinput_kb-2.1.1-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 231.3 kB
- Tags: CPython 3.11, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a20b74c16340dfee23453560010dfb976825a7ebde1311f97e3353fdd4769188
|
|
| MD5 |
a58abeaa8cd6927f65e0e813a36612f5
|
|
| BLAKE2b-256 |
b09ef46c7bed8d10d75f33a9356b9c42cda61a706c78668d3b4dd7590eb2744e
|