pub struct SpaceMouseThread { /* private fields */ }Expand description
Cross-platform SpaceMouse HID capture thread.
Handle to a background thread, which continuously
(re-)discovers a compatible device, reads its raw HID reports, and
forwards the resulting SpaceMouseEvents to the callback given to
SpaceMouseThread::new. The thread stops when the handle is dropped.
Constructing one is a safe no-op on a machine without a compatible device
connected. A device that is plugged in later, or unplugged and replugged,
is picked up automatically. If the HID subsystem cannot be initialized,
the thread ends immediately and reports nothing.
§Example
Print all events of a connected device until Enter is pressed:
use spacemouse::{SpaceMouseEvent, SpaceMouseThread};
use std::io;
fn on_event_callback(event: SpaceMouseEvent) {
match event {
SpaceMouseEvent::Connected => {
println!("connected");
}
SpaceMouseEvent::Disconnected => {
println!("disconnected");
}
SpaceMouseEvent::Translation { x, y, z } => {
println!("translation: {x} {y} {z}");
}
SpaceMouseEvent::Rotation { x, y, z } => {
println!("rotation: {x} {y} {z}");
}
_ => {}
}
}
// Start the background thread, which automatically connects to the device.
let thread = SpaceMouseThread::new(on_event_callback);
println!("Listening for SpaceMouse events. Press Enter to quit.");
let _ = io::stdin().read_line(&mut String::new());
// Dropping the handle stops the background thread.
drop(thread);Implementations§
Source§impl SpaceMouseThread
impl SpaceMouseThread
Sourcepub fn new<F>(on_event: F) -> Self
pub fn new<F>(on_event: F) -> Self
Start capturing in a new background thread.
on_event is invoked on the background thread, not the caller’s
thread, for every SpaceMouseEvent. Callers that need events on a
specific thread are responsible for the hop themselves.
Waits between scans (while no compatible device is open) using the
default scan interval. See SpaceMouseThread::with_rescan_interval
for a configurable scan period.
Sourcepub fn with_rescan_interval<F>(rescan_interval: Duration, on_event: F) -> Self
pub fn with_rescan_interval<F>(rescan_interval: Duration, on_event: F) -> Self
Like SpaceMouseThread::new, but waits rescan_interval between scans
while no compatible device is open.
Shorter intervals pick up a newly connected device sooner, at the cost of enumerating the system’s HID devices more often. Values below 100 ms are raised to 100 ms.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Whether a compatible device is currently open.