From b8912337f8675de7e3ca645c32a4e34d7f514479 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 18 Dec 2023 09:54:23 -0700 Subject: [PATCH] Add arrow keys and home/end --- src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index b704d38..72fa4b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use tiny_skia::{ColorU8, Paint, PixmapMut, PixmapPaint, PixmapRef, Rect, Transfo use winit::{ event::{ElementState, Event as WinitEvent, KeyEvent, Modifiers, WindowEvent}, event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy}, - keyboard::ModifiersState, + keyboard::{Key, ModifiersState, NamedKey}, window::WindowBuilder, }; @@ -340,7 +340,8 @@ fn main() { WindowEvent::KeyboardInput { event: KeyEvent { - text: Some(text), + logical_key, + text: text_opt, state: ElementState::Pressed, .. }, @@ -348,31 +349,59 @@ fn main() { }, window_id, } if window_id == window.id() => { - println!("{:?} {:?}", modifiers, text); + println!("{:?} {:?}", modifiers, text_opt); match ( modifiers.state().contains(ModifiersState::SUPER), modifiers.state().contains(ModifiersState::CONTROL), modifiers.state().contains(ModifiersState::ALT), ) { (true, _, _) => {} // Ignore super - (false, true, _) => { - // Control keys - if text.len() == 1 { + (false, true, _) => match text_opt { + Some(text) if text.len() == 1 => { + // Control keys let c = text.chars().next().unwrap_or_default(); if c >= 'a' && c <= 'z' { notifier.notify(vec![(c as u8) - b'a' + 1]); } } - } - (false, false, true) => { - // Alt keys - let mut bytes = text.as_bytes().to_vec(); - bytes.insert(0, b'\x1B'); - notifier.notify(bytes); - } - (false, false, false) => { - notifier.notify(text.as_bytes().to_vec()); - } + _ => {} + }, + (false, false, true) => match text_opt { + Some(text) => { + // Alt keys + //TODO: handle alt keys with no text + let mut bytes = text.as_bytes().to_vec(); + bytes.insert(0, b'\x1B'); + notifier.notify(bytes); + } + None => {} + }, + (false, false, false) => match text_opt { + Some(text) => { + notifier.notify(text.as_bytes().to_vec()); + } + None => match logical_key { + Key::Named(NamedKey::ArrowUp) => { + notifier.notify(b"\x1B[A".as_slice()); + } + Key::Named(NamedKey::ArrowDown) => { + notifier.notify(b"\x1B[B".as_slice()); + } + Key::Named(NamedKey::ArrowRight) => { + notifier.notify(b"\x1B[C".as_slice()); + } + Key::Named(NamedKey::ArrowLeft) => { + notifier.notify(b"\x1B[D".as_slice()); + } + Key::Named(NamedKey::End) => { + notifier.notify(b"\x1B[F".as_slice()); + } + Key::Named(NamedKey::Home) => { + notifier.notify(b"\x1B[H".as_slice()); + } + _ => {} + }, + }, } } WinitEvent::WindowEvent {