add key event parser; handles basic ascii and ctrl/alt combinations

This commit is contained in:
Crispy 2023-03-22 20:02:20 +01:00
parent 6ffb32c543
commit 8ffd34dcfa
3 changed files with 149 additions and 30 deletions

View file

@ -1,21 +1,17 @@
use ants::{event::Events, raw_mode};
use std::{
io::{stdin, Read},
time::{Duration, SystemTime},
use ants::{
event::{Event, Events, Key, Mod},
raw_mode,
};
fn main() {
raw_mode::enter().unwrap();
let start_time = SystemTime::now();
let mut buf = Vec::new();
while start_time.elapsed().unwrap() < Duration::from_secs(5) {
let mut b = [0u8];
if let Ok(_n) = stdin().read(&mut b) {
if b[0] != 0 {
buf.push(b[0]);
print!("[{}]:{}\n\r", b[0], String::from_utf8_lossy(&buf));
print!("Press Ctrl+q to exit.\r\n");
let mut events = Events::new();
loop {
if let Some(event) = events.next() {
print!("{:?}\r\n", event);
if let Event::Key(Key::Char('q'), Mod::Ctrl) = event {
break;
}
}
}