A cross-platform Rust library for querying keyboard key states without requiring a window context.
| Platform | API Used | Background Thread | Notes |
|---|---|---|---|
| Linux | evdev |
Yes (5ms poll rate) | Requires read access to /dev/input/event* devices |
| Windows | GetAsyncKeyState (Win32) |
No (on-demand) | No special permissions required |
| macOS | CGEventSourceKeyState (Core Graphics) |
No (on-demand) | Requires "Input Monitoring" permission |
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 -a -G 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.
The library currently supports:
- All letter keys (A-Z)
- Number keys (0-9)
- Function keys (F1-F12)
- Modifier keys (Shift, Ctrl, Alt, Caps Lock)
- Arrow keys
- Common symbol keys (brackets, punctuation, etc.)
- Space, Enter, Backspace, Tab, Escape
See the KeyCode enum for the complete list.
For detailed API documentation, run:
cargo doc --openOr visit docs.rs (once published).
This project is licensed under the MIT License - see the LICENSE file for details.