Skip to main content

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/uinput support)
  • Python 3.7+
  • Rust & Cargo (for building)
  • Root/Sudo Access (or configure udev rules so your user can write to /dev/uinput)

Installation & Building

This project uses maturin to build the Rust code into a Python module.

  1. Install maturin in your Python environment:
pip install maturin
  1. 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 with sudo or add a udev rule for the uinput group.
  • 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., to 800 or 1000).

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

rs_uinput_kb-1.1.0.tar.gz (24.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rs_uinput_kb-1.1.0-cp311-cp311-manylinux_2_34_x86_64.whl (228.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

File details

Details for the file rs_uinput_kb-1.1.0.tar.gz.

File metadata

  • Download URL: rs_uinput_kb-1.1.0.tar.gz
  • Upload date:
  • Size: 24.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.0

File hashes

Hashes for rs_uinput_kb-1.1.0.tar.gz
Algorithm Hash digest
SHA256 dbc0a651c09accf7457dce9f23731a3f1de3369687790154cfec978144755c25
MD5 37ba49db70570b4d25db73fa4862d83c
BLAKE2b-256 eccf0f7e4274287d2e2b63b7790ae6e599c0ae0c3b5cc88936e91d1e7765b555

See more details on using hashes here.

File details

Details for the file rs_uinput_kb-1.1.0-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for rs_uinput_kb-1.1.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1d3c8bcde11d91bf380539a6b85865ef5fd3ec02914fbedacce74028250b3623
MD5 7442621166a7db3891b8ff2fbf94dd2a
BLAKE2b-256 7ecf9564e96d30c749c59be93151aefb6f222ac7abb05b57403a31af9599db35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page