Skip to main content

Crate monio

Crate monio 

Source
Expand description

§monio

A pure Rust cross-platform input monitoring library with proper drag detection.

§Features

  • Cross-platform support (macOS, Windows, Linux)
  • Proper drag detection (distinguishes MouseDragged from MouseMoved)
  • Event grabbing (consume events to prevent them from reaching other apps)
  • Clean, Rust-idiomatic API with traits and enums
  • Thread-safe design with atomic state tracking
  • Event simulation support

§Quick Start

§Listening for Events

use monio::{listen, Event, EventType};

listen(|event: &Event| {
    match event.event_type {
        EventType::MouseDragged => {
            if let Some(mouse) = &event.mouse {
                println!("Dragging at ({}, {})", mouse.x, mouse.y);
            }
        }
        EventType::KeyPressed => {
            if let Some(kb) = &event.keyboard {
                println!("Key pressed: {:?}", kb.key);
            }
        }
        _ => {}
    }
}).expect("Failed to start hook");

§Grabbing Events (Blocking Keys/Mouse)

use monio::{grab, Event, EventType, Key};

grab(|event: &Event| {
    // Block the Escape key
    if event.event_type == EventType::KeyPressed {
        if let Some(kb) = &event.keyboard {
            if kb.key == Key::Escape {
                println!("Blocked Escape key!");
                return None; // Consume the event
            }
        }
    }
    Some(event.clone()) // Pass through
}).expect("Failed to start grab");

§Architecture

The library uses global atomic state tracking (see state module) to maintain button/modifier state across events. This enables proper detection of drag events - when a mouse move occurs while a button is held, we emit MouseDragged instead of MouseMoved.

Re-exports§

pub use display::DisplayInfo;
pub use display::Rect;
pub use display::SystemSettings;
pub use display::display_at_point;
pub use display::displays;
pub use display::primary_display;
pub use display::system_settings;
pub use error::Error;
pub use error::Result;
pub use event::Button;
pub use event::Event;
pub use event::EventType;
pub use event::KeyboardData;
pub use event::MouseData;
pub use event::ScrollDirection;
pub use event::WheelData;
pub use hook::EventHandler;
pub use hook::GrabHandler;
pub use hook::Hook;
pub use hook::grab;
pub use hook::listen;
pub use keycode::Key;

Modules§

channel
Channel-based event receiving for non-blocking event processing.
display
Display and system property queries.
error
Error types for the input hook library.
event
Event types and enums for the input hook library.
hook
Main Hook struct and EventHandler trait.
keycode
Virtual key code definitions.
state
Global state tracking for button mask and modifiers.

Functions§

key_press
Press a key.
key_release
Release a key.
key_tap
Press and release a key.
mouse_click
Click a mouse button (press and release).
mouse_move
Move the mouse to a position.
mouse_position
Get current mouse position as (x, y) coordinates.
mouse_press
Press a mouse button.
mouse_release
Release a mouse button.
simulate
Simulate an event.