https://github.com/micmonay/keybd_event-rs
Simulating keyboard on Linux, Windows and Mac OS in rust
https://github.com/micmonay/keybd_event-rs
keyboard keyboard-layout linux multiplatform rust simulation uinput
Last synced: 6 months ago
JSON representation
Simulating keyboard on Linux, Windows and Mac OS in rust
- Host: GitHub
- URL: https://github.com/micmonay/keybd_event-rs
- Owner: micmonay
- License: mit
- Created: 2019-08-10T11:28:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-11T05:43:58.000Z (over 6 years ago)
- Last Synced: 2025-07-02T04:19:01.897Z (7 months ago)
- Topics: keyboard, keyboard-layout, linux, multiplatform, rust, simulation, uinput
- Language: Rust
- Size: 42 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simulating keyboard on Linux, Windows and Mac OS in rust
On the next example, the library simulates key A, Z pressed.
**The keyboard layout on the computer is important!**
If you use a keyboard layout the US, you have corresponding keys, but if you use, for example, the french layout, you have another result.
```rust
extern crate keybd_event;
#[cfg(target_os = "linux")]
use std::thread::sleep;
#[cfg(target_os = "linux")]
use std::time::Duration;
use keybd_event::KeyboardKey::{KeyA,KeyZ};
use keybd_event::KeyBondingInstance;
fn main() {
let mut kb = KeyBondingInstance::new().unwrap();
#[cfg(target_os = "linux")]
sleep(Duration::from_secs(2));
kb.has_shift(true);
kb.add_keys(&[KeyA, KeyZ]);
kb.launching();
}
```

## Linux
On Linux this library use **uinput**, but generally the uinput is only for the root user.
The easy solution is executing on root user or change permission by `chmod`, but it is not good.
You can follow the next example, for more security.
```bash
sudo groupadd uinput
sudo usermod -a -G uinput my_username
sudo udevadm control --reload-rules
echo "SUBSYSTEM==\"misc\", KERNEL==\"uinput\", GROUP=\"uinput\", MODE=\"0660\"" | sudo tee /etc/udev/rules.d/uinput.rules
echo uinput | sudo tee /etc/modules-load.d/uinput.conf
```
Another subtlety on Linux, it is important after creating **KeyBondingInstance**, to waiting 2 seconds before running first keyboard actions
## Darwin (MAC OS)
This library depends on the frameworks Apple, I did not find a solution for cross-compilation.