Crate raw_input

Crate raw_input 

Source
Expand description

§raw-input

A cross-platform library for capturing and simulating global input events (keyboard and mouse).

§Core Components

  • Core: Manages the platform-specific event loop. Must be started to enable other features.
  • Listen: Provides a way to subscribe to global input events without blocking them.
  • Simulate: Allows programmatic injection of keyboard and mouse events.
  • Grab: Enables intercepting and optionally blocking input events from reaching other applications.
  • Display: Utilities for querying monitor information and cursor positions.

§Example

use std::thread;
use std::time::Duration;
 
use raw_input::{Core, Listen, Event};
 
fn main() {
    // 1. Start the core engine in a background thread 
    // (Crucial for processing Windows message loops)
    thread::spawn(|| {
        Core::start().expect("Failed to start raw-input core");
    });
 
    // 2. Subscribe to global events
    Listen::start();
    let handle = Listen::subscribe(|event| {
        match event {
            Event::KeyDown { key } => println!("Key pressed: {:?}", key),
            Event::MouseMove { delta } => println!("Mouse moved by: {}, {}", delta.x, delta.y),
            _ => {},
        }
    });
 
    // 3. Manage the subscription lifecycle
    thread::sleep(Duration::from_secs(5));
    handle.pause();    // Stop receiving events temporarily
 
    thread::sleep(Duration::from_secs(2));
    handle.resume();   // Start receiving events again
 
    thread::sleep(Duration::from_secs(2));
    handle.unsubscribe(); // Permanently remove the listener
    Listen::stop(); // Stop Listen
    Core::stop()
}

Structs§

Core
The system background engine manager.
Display
Screen and monitor information provider.
FloatPoint
A coordinate point using floating-point numbers, used for precise deltas or scaling.
Grab
Input interceptor for blocking or modifying events.
Listen
Global input listener for monitoring events.
MonitorInfo
Information about a connected physical monitor.
Point
A simple coordinate point using integers, typically for pixel positions.
Simulate
Input simulator for synthesizing events.
SubscriptionHandle
A handle that allows control over an active event subscription.

Enums§

CoreError
Errors that occur when trying to capture OS events.
Event
The main event enum containing all possible input actions.
Key
A high-level representation of keyboard keys.
MouseButton
Represents the standard buttons on a mouse.