input_query
A cross-platform Rust library for querying keyboard key states without requiring a window context.
Features
- 🖥️ Cross-platform: Supports Linux, Windows, and macOS
- 🚀 No window required: Query keyboard state directly from the OS
- 🎯 Simple API: Easy-to-use interface with minimal boilerplate
- 🔧 Platform-optimized: Uses native APIs for best performance on each platform
Platform Support
| 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 |
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
use ;
use thread;
use Duration;
Note: On Linux, input events are monitored in a background thread automatically. On Windows and macOS, the state is queried on-demand. You no longer need to call update_inputs() - the library handles updates automatically!
Example
You can run the included example with:
This will demonstrate real-time key detection. Press ESC to exit the example.
Platform-Specific Setup
Linux
On Linux, you need read access to input devices. You can either:
-
Add your user to the
inputgroup:Then log out and log back in.
-
Or run your application with appropriate permissions (not recommended for production).
macOS
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
Windows
No special setup required on Windows.
Supported Keys
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.
API Documentation
For detailed API documentation, run:
Or visit docs.rs (once published).
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
- Linux implementation uses the excellent evdev crate
- Windows implementation uses the windows crate
- macOS implementation uses the core-graphics crate