Expand description
A simple library for querying mouse and keyboard state without requiring an active window. Currently works in Windows, Linux, and macOS.
use device_query::{DeviceQuery, DeviceState, MouseState, Keycode};
let device_state = DeviceState::new();
let mouse: MouseState = device_state.get_mouse();
println!("Current Mouse Coordinates: {:?}", mouse.coords);
let keys: Vec<Keycode> = device_state.get_keys();
println!("Is A pressed? {}", keys.contains(&Keycode::A));
It’s also possible to listen for events.
use device_query::{DeviceEvents, DeviceEventsHandler};
use std::time::Duration;
let device_state = DeviceEventsHandler::new(Duration::from_millis(10))
.expect("Failed to start event loop");
// Register a key down event callback
// The guard is used to keep the callback alive
let _guard = device_state.on_mouse_move(|position| {
println!("Mouse position: {:#?}", position);
});
// Keep the main thread alive
loop {}
Re-exports§
pub use device_events::*;
pub use device_query::*;
pub use device_state::*;
pub use keymap::*;
pub use mouse_state::*;
Modules§
- device_
events - Devices events listeners.
- device_
query - Query functions.
- device_
state - DeviceState implementation.
- keymap
- List of keycodes.
- mouse_
state - Description of mouse coordinates and state of buttons.