Add arrow keys and home/end

This commit is contained in:
Jeremy Soller 2023-12-18 09:54:23 -07:00
parent 09a153bc8e
commit b8912337f8

View file

@ -21,7 +21,7 @@ use tiny_skia::{ColorU8, Paint, PixmapMut, PixmapPaint, PixmapRef, Rect, Transfo
use winit::{ use winit::{
event::{ElementState, Event as WinitEvent, KeyEvent, Modifiers, WindowEvent}, event::{ElementState, Event as WinitEvent, KeyEvent, Modifiers, WindowEvent},
event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy}, event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy},
keyboard::ModifiersState, keyboard::{Key, ModifiersState, NamedKey},
window::WindowBuilder, window::WindowBuilder,
}; };
@ -340,7 +340,8 @@ fn main() {
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
event: event:
KeyEvent { KeyEvent {
text: Some(text), logical_key,
text: text_opt,
state: ElementState::Pressed, state: ElementState::Pressed,
.. ..
}, },
@ -348,31 +349,59 @@ fn main() {
}, },
window_id, window_id,
} if window_id == window.id() => { } if window_id == window.id() => {
println!("{:?} {:?}", modifiers, text); println!("{:?} {:?}", modifiers, text_opt);
match ( match (
modifiers.state().contains(ModifiersState::SUPER), modifiers.state().contains(ModifiersState::SUPER),
modifiers.state().contains(ModifiersState::CONTROL), modifiers.state().contains(ModifiersState::CONTROL),
modifiers.state().contains(ModifiersState::ALT), modifiers.state().contains(ModifiersState::ALT),
) { ) {
(true, _, _) => {} // Ignore super (true, _, _) => {} // Ignore super
(false, true, _) => { (false, true, _) => match text_opt {
// Control keys Some(text) if text.len() == 1 => {
if text.len() == 1 { // Control keys
let c = text.chars().next().unwrap_or_default(); let c = text.chars().next().unwrap_or_default();
if c >= 'a' && c <= 'z' { if c >= 'a' && c <= 'z' {
notifier.notify(vec![(c as u8) - b'a' + 1]); notifier.notify(vec![(c as u8) - b'a' + 1]);
} }
} }
} _ => {}
(false, false, true) => { },
// Alt keys (false, false, true) => match text_opt {
let mut bytes = text.as_bytes().to_vec(); Some(text) => {
bytes.insert(0, b'\x1B'); // Alt keys
notifier.notify(bytes); //TODO: handle alt keys with no text
} let mut bytes = text.as_bytes().to_vec();
(false, false, false) => { bytes.insert(0, b'\x1B');
notifier.notify(text.as_bytes().to_vec()); 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 { WinitEvent::WindowEvent {