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.
- 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
Add to your Cargo.toml:
[dependencies]
hid-detector = "0.1"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(())
}let devices = detector.detect_with_callback(|device| {
println!("Detected: {} - {}", device.name(), device.vid_pid());
})?;- 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.
pub struct DetectionConfig {
pub keyboard_threshold: u64, // Default: 2
pub mouse_threshold: u64, // Default: 100
pub duration: Duration, // Default: 10 seconds
}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;
}pub enum DeviceType {
Mouse,
Keyboard,
Gamepad,
Unknown,
}cargo run --example detect- Windows only (uses Win32 Raw Input API)
MIT