Crates.io | async-hidg |
lib.rs | async-hidg |
version | 0.2.0 |
source | src |
created_at | 2024-05-11 05:01:00.786876 |
updated_at | 2024-05-11 05:01:00.786876 |
description | Linux USB HID Gadget emulation with async interface |
homepage | https://github.com/katyo/hidg-rs |
repository | https://github.com/katyo/hidg-rs |
max_upload_size | |
id | 1236639 |
size | 9,134 |
Rust crate for interfacing with Linux HID Gadget devices (/dev/hidgX). This crate supports non-tokio async runtimes like the smol and async-std.
Since all functionality is dependent on Linux function calls, this crate only compiles for Linux systems.
Keyboard input simulation:
use async_hidg::{Class, Device, Keyboard, Key, Led, StateChange};
#[smol_potat::main]
async fn main() -> std::io::Result<()> {
let mut device = Device::<Keyboard>::open(0).await?; // 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);
// Print pressed keys
println!("Keys: {:?}", input.pressed().collect::<Vec<Key>>());
// Send input report
device.input(&input).await?;
// Release left ctrl modifier
input.release_key(Key::LeftCtrl);
// Release key 'A'
input.release_key(Key::A);
// Send input report
device.input(&input).await?;
// Create output report
let mut output = Keyboard.output();
// Receive output report
device.output(&mut output).await?;
// Print lit LEDs
println!("LEDs: {:?}", output.lit().collect::<Vec<Led>>());
Ok(())
}
Mouse input simulation:
use async_hidg::{Button, Class, Device, Mouse, StateChange, ValueChange};
#[smol_potat::main]
async fn main() -> std::io::Result<()> {
let mut device = Device::<Mouse>::open("hidg0").await?; // open device
// Create input report
let mut input = Mouse.input();
// Press primary button
input.press_button(Button::Primary);
// Set pointer coordinates
input.change_pointer((150, 50), false);
// Send input report
device.input(&input).await?;
// Move pointer relatively
input.change_pointer((70, -30), true);
// Print pressed buttons
println!("Buttons: {:?}", input.pressed().collect::<Vec<Button>>());
// Release primary button
input.release_button(Button::Primary);
// Send input report
device.input(&input).await?;
Ok(())
}