1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyPress {
UnknownEscSeq,
Backspace,
BackTab,
BracketedPasteStart,
BracketedPasteEnd,
Char(char),
ControlDown,
ControlLeft,
ControlRight,
ControlUp,
Ctrl(char),
Delete,
Down,
End,
Enter,
Esc,
F(u8),
Home,
Insert,
Left,
Meta(char),
Null,
PageDown,
PageUp,
Right,
ShiftDown,
ShiftLeft,
ShiftRight,
ShiftUp,
Tab,
Up,
}
#[cfg(any(windows, unix))]
pub fn char_to_key_press(c: char) -> KeyPress {
if !c.is_control() {
return KeyPress::Char(c);
}
#[allow(clippy::match_same_arms)]
match c {
'\x00' => KeyPress::Ctrl(' '),
'\x01' => KeyPress::Ctrl('A'),
'\x02' => KeyPress::Ctrl('B'),
'\x03' => KeyPress::Ctrl('C'),
'\x04' => KeyPress::Ctrl('D'),
'\x05' => KeyPress::Ctrl('E'),
'\x06' => KeyPress::Ctrl('F'),
'\x07' => KeyPress::Ctrl('G'),
'\x08' => KeyPress::Backspace,
'\x09' => KeyPress::Tab,
'\x0a' => KeyPress::Ctrl('J'),
'\x0b' => KeyPress::Ctrl('K'),
'\x0c' => KeyPress::Ctrl('L'),
'\x0d' => KeyPress::Enter,
'\x0e' => KeyPress::Ctrl('N'),
'\x0f' => KeyPress::Ctrl('O'),
'\x10' => KeyPress::Ctrl('P'),
'\x12' => KeyPress::Ctrl('R'),
'\x13' => KeyPress::Ctrl('S'),
'\x14' => KeyPress::Ctrl('T'),
'\x15' => KeyPress::Ctrl('U'),
'\x16' => KeyPress::Ctrl('V'),
'\x17' => KeyPress::Ctrl('W'),
'\x18' => KeyPress::Ctrl('X'),
'\x19' => KeyPress::Ctrl('Y'),
'\x1a' => KeyPress::Ctrl('Z'),
'\x1b' => KeyPress::Esc,
'\x1c' => KeyPress::Ctrl('\\'),
'\x1d' => KeyPress::Ctrl(']'),
'\x1e' => KeyPress::Ctrl('^'),
'\x1f' => KeyPress::Ctrl('_'),
'\x7f' => KeyPress::Backspace,
_ => KeyPress::Null,
}
}
#[cfg(test)]
mod tests {
use super::{char_to_key_press, KeyPress};
#[test]
fn char_to_key() {
assert_eq!(KeyPress::Esc, char_to_key_press('\x1b'));
}
}