A cross-platform Rust library for querying keyboard key states without requiring a window context.
use input_query::{InputHandler, KeyCode};
use std::thread;
use std::time::Duration;
fn main() {
let handler = InputHandler::new();
loop {
if handler.is_pressed(KeyCode::KeyEsc) {
println!("Escape key is pressed!");
break;
}
if handler.is_pressed(KeyCode::KeySpace) {
println!("Space bar is pressed!");
}
// Your application logic here
thread::sleep(Duration::from_millis(10));
}
}On Linux, you need read access to input devices.
Add your user to the input group:
sudo usermod -aG input $USERThen log out and log back in.
On macOS, the application needs "Input Monitoring" permission:
- Run your application
- When prompted, or manually go to: System Preferences → Security & Privacy → Privacy → Input Monitoring
- Add and enable your application
No special setup required on Windows.