Expand description
§input_query
A cross-platform Rust library for querying keyboard key states without requiring a window context.
§Usage
use input_query::{InputHandler, KeyCode};
use std::thread;
use std::time::Duration;
let handler = InputHandler::new().expect("can't read the keyboard");
loop {
if handler.is_pressed(KeyCode::KeyEsc) {
println!("Escape key is pressed!");
break;
}
if handler.is_pressed(KeyCode::KeySpace) {
println!("Space bar is pressed!");
}
thread::sleep(Duration::from_millis(10));
}To check several keys at once, InputHandler::snapshot reads them all
together, which is cheaper than many is_pressed
calls on Linux.
Keys are read whichever window has focus. An application that knows when it
has focus can pass that on with InputHandler::set_focused, so keys only
count while it does.
§Note on Permissions
- Linux: Requires read access to
/dev/input/event*devices. You may need to add your user to theinputgroup or run with appropriate permissions. Without it,InputHandler::newreturnsError::PermissionDenied. - macOS: Requires accessibility permissions. The application may need to be granted “Input Monitoring” permission in System Preferences → Security & Privacy → Privacy.
- Windows: No special permissions required.
On other platforms, InputHandler::new returns Error::Unsupported.
Re-exports§
pub use input_handler::Error;pub use input_handler::InputHandler;pub use input_handler::KeyCode;pub use input_handler::Snapshot;
Modules§
- input_
handler - Platform-specific input handler implementations and key code definitions.