mouse-codes
A comprehensive Rust library for cross-platform mouse button code mapping and conversion. Provides standardized mouse button definitions and platform-specific code mappings for Windows, Linux, and macOS.
Features
- Cross-Platform Support: Unified API for Windows, Linux, and macOS
- Standard Button Mapping: Pre-defined mappings for common mouse buttons
- Custom Button Support: Extensible system for custom mouse buttons
- Flexible Parsing: Case-insensitive parsing with alias support
- Multiple Backends: Choose between
std::collections::HashMaporphffor performance - Serde Support: Optional serialization/deserialization support
- Extended Buttons: Support for extra mouse buttons (feature-gated)
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Optional Features
serde: Enables serialization supportphf: Uses perfect hash functions for better performanceextended: Enables support for extra mouse buttons (Extra9, Extra10)
Quick Start
use ;
Usage Examples
Basic Button Operations
use ;
// Convert buttons to platform-specific codes
let windows_code = Left.to_code; // 1
let linux_code = Right.to_code; // 3
let macos_code = Middle.to_code; // 2
// Convert codes back to buttons
let button1 = from_code; // Some(Button::Left)
let button2 = from_code; // Some(Button::X1)
String Parsing
use ;
// Case-insensitive parsing
let button1 = parse_button_ignore_case.unwrap; // Button::Left
let button2 = parse_button_ignore_case.unwrap; // Button::Middle
// Parsing with aliases
let button3 = parse_button_with_aliases.unwrap; // Button::Left
let button4 = parse_button_with_aliases.unwrap; // Button::X1
Custom Button Mapping
use ;
let mut custom_map = new;
// Add a custom button with platform-specific codes
custom_map.add_button.unwrap;
// Use the custom mapping
let code = custom_map.get_code_for_button; // Some(16)
let button = custom_map.from_code; // Some(CustomButton::Custom(...))
Mouse Event Parsing
use parse_mouse_input;
let event1 = parse_mouse_input.unwrap;
let event2 = parse_mouse_input.unwrap;
let event3 = parse_mouse_input.unwrap;
Platform Mappings
| Button | Windows | Linux | macOS |
|---|---|---|---|
| Left | 1 | 1 | 0 |
| Right | 2 | 3 | 1 |
| Middle | 4 | 2 | 2 |
| X1 | 5 | 8 | 3 |
| X2 | 6 | 9 | 4 |
| Extra3 | 7 | 10 | 5 |
| Extra4 | 8 | 11 | 6 |
| Extra5 | 9 | 12 | 7 |
| Extra6 | 10 | 13 | 8 |
| Extra7 | 11 | 14 | 9 |
| Extra8 | 12 | 15 | 10 |
Performance
The crate offers two implementation backends:
- HashMap (default): Uses
std::collections::HashMap - PHF: Uses perfect hash functions for compile-time optimized lookups
Enable the phf feature for better performance:
[]
= { = "0.1", = ["phf"] }
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.