evdev-rs/examples/virtual_keyboard.rs
Jeff Hiner 6c1add8b73
Add support for virtual uinput devices (#37)
* Prototype support for virtual uinput devices

* Tidy uinput a bit

* Have all the evtest examples use the same args/prompt code

* Switch to libc uinput* structs

* Use InputId in uinput, use libc _CNT constants

* Don't use align_to_mut

* Only check /dev/input/eventN files

* Spelling fixup

* Fix compilation error

* Ah, there we go. Much better.

Co-authored-by: Jeff Hiner <jeff-hiner@users.noreply.github.com>
Co-authored-by: Noah <33094578+coolreader18@users.noreply.github.com>
2021-03-19 09:19:51 -06:00

36 lines
1.1 KiB
Rust

// 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));
}
}