Expand description
§Input Handling System
This module provides comprehensive input handling capabilities for terminal-based applications. It abstracts over platform-specific input mechanisms and provides a unified interface for keyboard and mouse input.
§Features
- Cross-platform Input: Works consistently across Windows, macOS, and Linux
- Multiple Input Modes: Supports blocking, non-blocking, and timeout-based input
- Event-driven Architecture: Converts low-level input into high-level events
- Configurable Polling: Adjustable polling rates for different performance needs
- Future Mouse Support: Designed to support mouse input (planned feature)
§Input Modes
The input system supports three primary modes of operation:
§1. Polling Mode (Non-blocking)
Immediately returns whether input is available, never blocks execution. Perfect for game loops and real-time applications.
§2. Timeout Mode (Semi-blocking)
Waits for input up to a specified duration, then returns with or without input. Ideal for responsive UIs that need to balance input handling with other tasks.
§3. Blocking Mode (Fully blocking)
Waits indefinitely until input is available. Best for menu systems and turn-based applications.
§Examples
§Basic Input Polling
use minui::input::KeyboardHandler;
use minui::Event;
let keyboard = KeyboardHandler::new();
// Non-blocking check for input
if let Some(event) = keyboard.poll()? {
match event {
Event::Character('q') => println!("Quit requested"),
Event::KeyUp => println!("Up arrow pressed"),
_ => println!("Other input: {:?}", event),
}
}§Timeout-based Input
use minui::input::KeyboardHandler;
use minui::Event;
use std::time::Duration;
let keyboard = KeyboardHandler::new();
// Wait up to 1 second for input
let event = keyboard.get_input(Duration::from_secs(1))?;
match event {
Event::Unknown => println!("Timeout or no input"),
other => println!("Got input: {:?}", other),
}§Blocking Input
use minui::input::KeyboardHandler;
let keyboard = KeyboardHandler::new();
println!("Press any key to continue...");
let event = keyboard.wait_for_input()?;
println!("You pressed: {:?}", event);§Performance Tuning
The input system allows you to adjust polling rates based on your application’s needs:
use minui::input::KeyboardHandler;
let mut keyboard = KeyboardHandler::new();
// For high-performance games (poll every 1ms)
keyboard.set_poll_rate(1);
// For normal applications (poll every 10ms)
keyboard.set_poll_rate(10);
// For low-power applications (poll every 50ms)
keyboard.set_poll_rate(50);Re-exports§
pub use scroll::ScrollDirection;pub use scroll::Scroller;
Modules§
- scroll
- Scrolling Module
Structs§
- Click
Tracker - Tracks click timing and position for double-click detection.
- Combined
Input Handler - Combined input handler for both keyboard and mouse input.
- Keyboard
Handler - Handles keyboard input with configurable polling rates, multiple input modes, and custom keybind support.
- Mouse
Handler - Handles mouse input with configurable polling rates and event tracking.
Enums§
- Keybind
Action - Actions that can be bound to key combinations.