evdev-rs/examples/virtual_keyboard.rs

36 lines
1.1 KiB
Rust
Raw Normal View History

// Create a virtual keyboard, just while this is running.
// Generally this requires root.
use evdev::{uinput::VirtualDeviceBuilder, AttributeSet, EventType, InputEvent, Key};
use std::thread::sleep;
use std::time::Duration;
fn main() -> std::io::Result<()> {
let mut keys = AttributeSet::<Key>::new();
keys.insert(Key::BTN_DPAD_UP);
let mut device = VirtualDeviceBuilder::new()?
.name("Fake Keyboard")
.with_keys(&keys)?
.build()
.unwrap();
let type_ = EventType::KEY;
// Note this will ACTUALLY PRESS the button on your computer.
// Hopefully you don't have BTN_DPAD_UP bound to anything important.
let code = Key::BTN_DPAD_UP.code();
println!("Waiting for Ctrl-C...");
loop {
let down_event = InputEvent::new(type_, code, 1);
device.emit(&[down_event]).unwrap();
println!("Pressed.");
sleep(Duration::from_secs(2));
let up_event = InputEvent::new(type_, code, 0);
device.emit(&[up_event]).unwrap();
println!("Released.");
sleep(Duration::from_secs(2));
}
}