MCPcopy Create free account
hub / github.com/Henktorius/float / encode_key

Function encode_key

src/input.rs:3–31  ·  view source on GitHub ↗
(key: KeyEvent)

Source from the content-addressed store, hash-verified

1use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
2
3pub fn encode_key(key: KeyEvent) -> Option<Vec<u8>> {
4 if key.kind == KeyEventKind::Release {
5 return None;
6 }
7 match key.code {
8 KeyCode::Char(c) => {
9 if key.modifiers.contains(KeyModifiers::CONTROL) {
10 let upper = c.to_ascii_uppercase();
11 if upper.is_ascii_alphabetic() {
12 let byte = (upper as u8) - b'A' + 1;
13 return Some(vec![byte]);
14 }
15 None
16 } else {
17 let mut buf = [0u8; 4];
18 Some(c.encode_utf8(&mut buf).as_bytes().to_vec())
19 }
20 }
21 KeyCode::Enter => Some(b"\r".to_vec()),
22 KeyCode::Backspace => Some(vec![0x7f]),
23 KeyCode::Tab => Some(b"\t".to_vec()),
24 KeyCode::Esc => Some(vec![0x1b]),
25 KeyCode::Up => Some(b"\x1b[A".to_vec()),
26 KeyCode::Down => Some(b"\x1b[B".to_vec()),
27 KeyCode::Right => Some(b"\x1b[C".to_vec()),
28 KeyCode::Left => Some(b"\x1b[D".to_vec()),
29 _ => None,
30 }
31}

Callers 1

handle_keyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected