Expand description
§iohidmanager
Safe Rust bindings for Apple’s IOKit HID subsystem on macOS — enumerate connected mice, keyboards, gamepads, and other HID devices.
Status: actively developed. v0.7 adds an executor-agnostic
asyncfeature with 7 stream types wrapping every IOKit HID callback API; v0.6 ships a Swift bridge forIOKit/hid, keeps the raw C surface behind theraw-ffifeature (enabled by default), and adds logical-area coverage for Manager, Device, Element, Value, Transaction, Queue, Keys, Usage, ServicePlugIn, and EventSystem.
§Quick start
use iohidmanager::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let manager = HidManager::new()?;
manager.set_device_matching(None)?;
for d in manager.devices() {
println!(
"[{:04x}:{:04x}] {} — {} ({})",
d.vendor_id.unwrap_or(0),
d.product_id.unwrap_or(0),
d.manufacturer.as_deref().unwrap_or("?"),
d.product.as_deref().unwrap_or("?"),
d.transport.as_deref().unwrap_or("?"),
);
}
let kb_or_mouse = [
DeviceMatch::usage(HidUsage::Keyboard),
DeviceMatch::usage(HidUsage::Mouse),
];
manager.set_device_matching_multiple(&kb_or_mouse)?;
println!("{} keyboards/mice connected", manager.devices().len());
Ok(())
}§Highlights in v0.7
asyncCargo feature exposing 7 executor-agnostic stream types wrapping everyIOKitHID callback API.- Each stream spawns a dedicated background
CFRunLoopthread — no async runtime required. - Stream types:
ManagerInputValueStream,ManagerDeviceMatchingStream,ManagerDeviceRemovalStream,ManagerInputReportStream,DeviceRemovalStream,DeviceInputValueStream,QueueValueStream.
§Highlights in v0.6
- Swift bridge target under
swift-bridge/with one Swift file per logical area. raw-ffiCargo feature (enabled by default) re-exporting the rawIOKit/hiddeclarations.- New safe transaction and queue wrappers:
HidTransaction,HidTransactionDirection,HidQueue, andHidQueueOptions. - Manager matching helpers for input-value filtering.
- Device helpers for multi-value reads/writes, removal callbacks, and timeout-based value/report setters.
- Element attach/detach helpers.
unsafe { HidValue::from_bytes_no_copy(...) }bridged through Swift.- Generated
keysandusagecatalogs, event-system constants, and service-plugin UUID helpers. - 15 runnable examples and per-area smoke tests.
§Feature flags
raw-ffi(default) — re-export the rawiohidmanager::ffimodule.async— enable theasync_apimodule with 7 executor-agnostic stream types.
Disable default features if you only want the safe surface:
cargo add iohidmanager --no-default-features§Area guide
- Manager — device matching, properties, enumeration, and manager-level filtering.
- Device — properties, reports, synchronous values, multi-value helpers, and removal subscriptions.
- Element — hierarchy traversal, metadata, and attach/detach relationships.
- Value — integer/byte constructors, no-copy bridge constructor, typed accessors.
- Transaction — batched input/output element transactions.
- Queue — buffered value delivery and queue depth management.
- Keys — generated string/numeric key catalog from
IOHIDKeys.h,IOHIDDeviceKeys.h, andIOHIDProperties.h. - Usage — generated usage/page constant catalog from
IOHIDUsageTables.h. - ServicePlugIn — UUID helpers for the
IOHIDDevicePlugIn.hinterfaces. - EventSystem —
IOHIDEventServiceKeys.handIOHIDEventServiceTypes.hconstants.
§Examples
The crate ships 17 examples, including:
06_manager_callbacks07_device_roundtrip08_element_attachment09_value_constructors10_transaction_roundtrip11_queue_roundtrip12_keys_catalog13_usage_catalog14_service_plugin_ids15_event_system_catalog17_async_stream(requires--features async)
Run them all with:
for ex in examples/*.rs; do cargo run --example "$(basename "$ex" .rs)"; done§Pipeline composition
iohidmanager (enumerate + inspect + subscribe) ──► your custom dispatch
│
├─► gamepad-mapper
├─► macropad-driver
└─► hardware-token authenticationPairs naturally with cgevents (synthesise events triggered by HID input) and carbonhotkey (more focused: just global hotkeys).
§Roadmap
-
HidManager::new()+ open -
DeviceMatch/ElementMatchbuilders + multi-match dictionaries - device enumeration and live device handles
- report/value callbacks on devices
- manager input-value matching helpers
-
HidTransactionandHidQueue -
unsafe HidValue::from_bytes_no_copy - generated keys/usage catalogs
- service-plugin UUID helpers and event-system constants
-
Swift bridge +
raw-ffifeature split
§License
Licensed under either of Apache-2.0 or MIT at your option.
§API documentation
Safe Rust bindings for Apple’s IOKit HID subsystem on macOS — enumerate connected mice, keyboards, gamepads, and other HID devices.
Re-exports§
pub use error::HidError;pub use hid::event_system;pub use hid::keys;pub use hid::service_plugin;pub use hid::usage;pub use hid::DeviceMatch;pub use hid::DeviceRemovalSubscription;pub use hid::ElementMatch;pub use hid::HidCollectionType;pub use hid::HidDevice;pub use hid::HidDeviceInfo;pub use hid::HidElement;pub use hid::HidElementType;pub use hid::HidInputReport;pub use hid::HidManager;pub use hid::HidManagerOptions;pub use hid::HidQueue;pub use hid::HidQueueOptions;pub use hid::HidReportType;pub use hid::HidTransaction;pub use hid::HidTransactionDirection;pub use hid::HidTransactionOptions;pub use hid::HidUsage;pub use hid::HidValue;pub use hid::HidValueScale;pub use hid::ManagerDeviceSubscription;pub use hid::ManagerReportSubscription;pub use hid::ManagerValueSubscription;pub use hid::QueueValueAvailableSubscription;pub use hid::ReportSubscription;pub use hid::TimestampedReportSubscription;pub use hid::ValueSubscription;