Windows HID device detection library for Rust.
Detect and identify HID devices (keyboards, mice, gamepads) by their VID/PID using the Windows Raw Input API.
Features
- Simple API for device detection
- Retrieves Windows friendly names
- Extracts VID/PID from device paths
- Configurable detection thresholds
- Real-time detection callbacks
- Support for keyboards, mice, and gamepads
Usage
Add to your Cargo.toml:
[dependencies] hid-detector = "0.1"
Basic Example
use hid_detector::{DetectionConfig, DeviceDetector}; use std::time::Duration; fn main() -> Result<(), Box<dyn std::error::Error>> { let config = DetectionConfig { keyboard_threshold: 2, mouse_threshold: 100, duration: Duration::from_secs(10), }; let detector = DeviceDetector::new(config)?; let devices = detector.detect()?; for device in devices { println!("{}: {} ({})", device.name(), device.vid_pid(), device.device_type() ); } Ok(()) }
With Real-Time Callback
let devices = detector.detect_with_callback(|device| { println!("Detected: {} - {}", device.name(), device.vid_pid()); })?;
How It Works
- Keyboards: Detected after 2 key presses (default)
- Mice: Detected after 100 movement/click events (default)
- Gamepads: Detected after 100 input events (default)
The library listens to Raw Input events for a configured duration and confirms devices once they cross the input threshold.
API
DetectionConfig
pub struct DetectionConfig { pub keyboard_threshold: u64, // Default: 2 pub mouse_threshold: u64, // Default: 100 pub duration: Duration, // Default: 10 seconds }
Device
impl Device { pub fn name(&self) -> String; pub fn vid_pid(&self) -> String; pub fn path(&self) -> &str; pub fn device_type(&self) -> DeviceType; pub fn vid(&self) -> Option<u16>; pub fn pid(&self) -> Option<u16>; pub fn input_count(&self) -> u64; }
DeviceType
pub enum DeviceType { Mouse, Keyboard, Gamepad, Unknown, }
Running the Example
cargo run --example detect
Platform Support
- Windows only (uses Win32 Raw Input API)
License
MIT