Debian Codegraph @

examples/virtual_keyboard.rs

raw symbols SCIP
1 // Create a virtual keyboard, just while this is running.
2 // Generally this requires root.
3
4 use evdev::KeyEvent;
5 use evdev::{uinput::VirtualDevice, AttributeSet, EventType, InputEvent, KeyCode};
6 use std::thread::sleep;
7 use std::time::Duration;
8
9 fn main() -> std::io::Result<()> {
10 let mut keys = AttributeSet::<KeyCode>::new();
11 keys.insert(KeyCode::BTN_DPAD_UP);
12
13 let mut device = VirtualDevice::builder()?
14 .name("Fake Keyboard")
15 .with_keys(&keys)?
16 .build()
17 .unwrap();
18
19 for path in device.enumerate_dev_nodes_blocking()? {
20 let path = path?;
21 println!("Available as {}", path.display());
22 }
23
24 // Note this will ACTUALLY PRESS the button on your computer.
25 // Hopefully you don't have BTN_DPAD_UP bound to anything important.
26 let code = KeyCode::BTN_DPAD_UP.code();
27
28 println!("Waiting for Ctrl-C...");
29 loop {
30 // this guarantees a key event
31 let down_event = *KeyEvent::new(KeyCode(code), 1);
32 device.emit(&[down_event]).unwrap();
33 println!("Pressed.");
34 sleep(Duration::from_secs(2));
35
36 // alternativeley we can create a InputEvent, which will be any variant of InputEvent
37 // depending on the type_ value
38 let up_event = InputEvent::new(EventType::KEY.0, code, 0);
39 device.emit(&[up_event]).unwrap();
40 println!("Released.");
41 sleep(Duration::from_secs(2));
42 }
43 }