Expand description
§HID Gadget Emulation in Rust
Rust crate for interfacing with Linux HID Gadget devices (/dev/hidgX).
Since all functionality is dependent on Linux function calls, this crate only compiles for Linux systems.
§Crates
- hidg-core - core abstractions and low level interface (not for end users)
- hidg - std interface which supports synchronous operation only
- tokio-hidg - async interface for tokio runtime
- async-hidg - async interface for other runtimes
§Features
- fromstr - implements core::str::FromStr implementation for some types
- display - implements std::fmt::Display implementation for some types
- phf - use phf in core::str::FromStr trait implementations
- serde - enables serde support for some types
- keyboard - enables keyboard class support
- mouse - enables mouse class support
§Usage examples
Keyboard input simulation:
use hidg::{Class, Device, Keyboard, Key, Led, StateChange};
fn main() -> std::io::Result<()> {
let mut device = Device::<Keyboard>::open(0)?; // open device
// Create input report
let mut input = Keyboard.input();
// Press left ctrl modifier
input.press_key(Key::LeftCtrl);
// Press key 'A'
input.press_key(Key::A);
// Send input report
device.input(&input)?;
// Get pressed keys
println!("Keys: {:?}", input.pressed().collect::<Vec<Key>>());
// Release left ctrl modifier
input.release_key(Key::LeftCtrl);
// Release key 'A'
input.release_key(Key::A);
// Send input report
device.input(&input)?;
// Create output report
let mut output = Keyboard.output();
// Receive output report
device.output(&mut output)?;
// Print lit LEDs
println!("LEDs: {:?}", output.lit().collect::<Vec<Led>>());
Ok(())
}
Mouse input simulation:
use hidg::{Button, Class, Device, Mouse, StateChange, ValueChange};
fn main() -> std::io::Result<()> {
let mut device = Device::<Mouse>::open("hidg0")?; // open device
// Create input report
let mut input = Mouse.input();
// Press primary button
input.press_button(Button::Primary);
// Update pointer coordinates
input.change_pointer((150, 50), false);
// Send input report
device.input(&input)?;
// Move pointer relatively
input.change_pointer((70, -30), true);
// Get pressed buttons
println!("Buttons: {:?}", input.pressed().collect::<Vec<Button>>());
// Release primary button
input.release_button(Button::Primary);
// Send input report
device.input(&input)?;
Ok(())
}
Structs§
- Button mask
- HID Gadget Device
- Changes between keyboard input reports
- Keyboard HID class
- Keyboard input report
- Keyboard output report
- Changes between keyboard output reports
- LED mask
- Modifier mask
- Mouse HID class
- Mouse input report
- Changes between mouse input reports
- Keyboard output report
- Key/button/LED state change event
- Pointer/cursor position change event
Enums§
- Button code
- Key code
- LED code
- Change between mouse input reports
Traits§
- Device class trait
Type Aliases§
- A specialized
Result
type for I/O operations.