Created
November 7, 2021 21:57
-
-
Save P1n3appl3/362c6a0e6e38da387f011db1587d6123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use evdev::{self, EventType, InputEventKind}; | |
use mio::unix::SourceFd; | |
use mio::{Events, Interest, Poll, Token}; | |
use std::io; | |
use std::os::unix::io::AsRawFd; | |
fn main() -> io::Result<()> { | |
let devices = evdev::enumerate().collect::<Vec<_>>(); | |
// for d in devices { | |
// println!( | |
// "Name: {:?}, path: {:?}, events: {:?}", | |
// d.name(), | |
// d.physical_path(), | |
// d.supported_events() | |
// ); | |
// } | |
let mut devices = devices | |
.into_iter() | |
.filter(|d| { | |
let types = d.supported_events(); | |
// TODO: consider a better heuristic for keyboards than "things with | |
// key repeat and no mouse or scroll wheel". maybe use `properties`? | |
// on the other hand we could just be conservative and accept more | |
// devices and throw out their events when they're not key presses | |
types.contains(EventType::KEY) | |
&& types.contains(EventType::REPEAT) | |
&& !types.contains(EventType::RELATIVE) | |
}) | |
.collect::<Vec<_>>(); | |
let fds = devices.iter().map(|d| d.as_raw_fd()).collect::<Vec<_>>(); | |
let mut sources = fds.iter().map(|d| SourceFd(&d)).collect::<Vec<_>>(); | |
let mut poll = Poll::new()?; | |
let mut events = Events::with_capacity(1024); | |
for (i, source) in sources.iter_mut().enumerate() { | |
poll.registry() | |
.register(source, Token(i), Interest::READABLE)?; | |
} | |
loop { | |
poll.poll(&mut events, None)?; | |
for event in &events { | |
let idx = event.token().0; | |
for ev in devices[idx].fetch_events().unwrap() { | |
if let InputEventKind::Key(k) = ev.kind() { | |
println!( | |
"{:?} - {}", | |
k, | |
if ev.value() == 0 { | |
"released" | |
} else { | |
"pressed" | |
} | |
); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment