Register device event callbacks
masterInstead of polling state, you can use DeviceEventsHandler to register callbacks that trigger on specific device events.
- Initialize the handler using
DeviceEventsHandler::new(Duration)specifying the polling interval. - Use methods like
on_mouse_moveto register listeners. - The callbacks are managed via a 'guard' pattern: the callbacks are automatically deregistered when the returned guard object goes out of scope.
extern crate device_query;
use device_query::{DeviceEvents, DeviceEventsHandler, Keycode, MouseButton, MousePosition};
use std::thread;
use std::time::Duration;
fn main() {
// Initialize the event handler with a sleep duration of 10 milliseconds
let event_handler = DeviceEventsHandler::new(Duration::from_millis(10))
.expect("Could not initialize event loop");
// Register callbacks for various events
// The callbacks will be automatically deregistered when they go out of scope
let _mouse_move_guard = event_handler.on_mouse_move(|position: &MousePosition| {
println!("Mouse moved to position: {:?}", position);
});
// Keep the main thread alive to continue receiving events
loop {
thread::sleep(Duration::from_secs(1000));
}
}