Compare commits

...

2 Commits

Author SHA1 Message Date
mars 71202752f8 Arrow keys, home, delete, page up/down, and insert 2022-10-05 16:57:42 -06:00
mars a19fca364e Debug-log received characters 2022-10-05 15:51:47 -06:00
1 changed files with 41 additions and 57 deletions

View File

@ -343,20 +343,53 @@ impl App {
}
}
pub fn virtual_keycode_to_string(
keycode: winit::event::VirtualKeyCode,
) -> Option<&'static str> {
use winit::event::VirtualKeyCode::*;
match keycode {
Up => Some("\x1b[A"),
Down => Some("\x1b[B"),
Right => Some("\x1b[C"),
Left => Some("\x1b[D"),
Home => Some("\x1b[1~"),
Insert => Some("\x1b[2~"),
Delete => Some("\x1b[3~"),
End => Some("\x1b[4~"),
PageUp => Some("\x1b[5~"),
PageDown => Some("\x1b[6~"),
_ => None,
}
}
pub fn on_keyboard_input(&mut self, input: &winit::event::KeyboardInput) {
if input.state == winit::event::ElementState::Pressed {
if let Some(keycode) = input.virtual_keycode {
if let Some(c) = virtual_keycode_to_byte(keycode) {
self.on_received_character(c as char);
if let Some(input) = Self::virtual_keycode_to_string(keycode) {
self.send_input(input);
}
}
}
}
pub fn on_received_character(&mut self, c: char) {
let mut bytes = [0u8; 4];
let len = c.encode_utf8(&mut bytes).len();
let cow = Cow::Owned(bytes[0..len].into());
log::debug!("Received character: {:?}", c);
match c {
'\u{7f}' => {
log::debug!("Ignoring.");
return;
}
_ => {}
}
let string = c.to_string();
self.send_input(string.as_str());
}
pub fn send_input(&mut self, input: &str) {
let bytes = input.as_bytes();
let cow = Cow::Owned(bytes.to_owned().into());
self.term_channel.send(TermMsg::Input(cow)).unwrap();
}
@ -378,58 +411,6 @@ impl App {
}
}
fn virtual_keycode_to_byte(keycode: winit::event::VirtualKeyCode) -> Option<u8> {
use winit::event::VirtualKeyCode::*;
match keycode {
A => Some(b'a'),
B => Some(b'b'),
C => Some(b'c'),
D => Some(b'd'),
E => Some(b'e'),
F => Some(b'f'),
G => Some(b'g'),
H => Some(b'h'),
I => Some(b'i'),
J => Some(b'j'),
K => Some(b'k'),
L => Some(b'l'),
M => Some(b'm'),
N => Some(b'n'),
O => Some(b'o'),
P => Some(b'p'),
Q => Some(b'q'),
R => Some(b'r'),
S => Some(b's'),
T => Some(b't'),
U => Some(b'u'),
V => Some(b'v'),
W => Some(b'w'),
X => Some(b'x'),
Y => Some(b'y'),
Z => Some(b'z'),
Key1 => Some(b'1'),
Key2 => Some(b'2'),
Key3 => Some(b'3'),
Key4 => Some(b'4'),
Key5 => Some(b'5'),
Key6 => Some(b'6'),
Key7 => Some(b'7'),
Key8 => Some(b'8'),
Key9 => Some(b'('),
Key0 => Some(b')'),
Apostrophe => Some(b'\''),
Period => Some(b'.'),
Escape => Some(0x1b),
Backslash => Some(b'\\'),
Colon => Some(b':'),
Return => Some(b'\r'),
Back => Some(0x08),
Tab => Some(b'\t'),
Space => Some(b' '),
_ => None,
}
}
fn main() {
env_logger::init();
@ -459,6 +440,9 @@ fn main() {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
WindowEvent::KeyboardInput { input, .. } => {
app.on_keyboard_input(&input);
}
WindowEvent::ReceivedCharacter(c) => {
app.on_received_character(c);
}