1use crate::rdev::Key;
2
3pub const RESERVED_UNKNOWN_CODE: u32 = 0;
4
5macro_rules! decl_keycodes {
6 ($($key:ident, $code:expr),*) => {
7 pub fn code_from_key(key: Key) -> Option<&'static str> {
8 match key {
9 $(
10 Key::$key => Some($code),
11 )*
12 _ => None,
13 }
14 }
15
16 pub fn key_from_code(code: &str) -> Key {
17 match code {
18 $(
19 $code => Key::$key,
20 )*
21 _ => Key::Unknown(RESERVED_UNKNOWN_CODE)
22 }
23 }
24 };
25}
26
27decl_keycodes! {
29 Alt, "AltLeft",
30 AltGr, "AltRight",
31 Backspace, "Backspace",
32 CapsLock, "CapsLock",
33 ControlLeft, "ControlLeft",
34 ControlRight, "ControlRight",
35 Delete, "Delete",
36 UpArrow, "ArrowUp",
37 DownArrow, "ArrowDown",
38 LeftArrow, "ArrowLeft",
39 RightArrow, "ArrowRight",
40 End, "End",
41 Escape, "Escape",
42 F1, "F1",
43 F2, "F2",
44 F3, "F3",
45 F4, "F4",
46 F5, "F5",
47 F6, "F6",
48 F7, "F7",
49 F8, "F8",
50 F9, "F9",
51 F10, "F10",
52 F11, "F11",
53 F12, "F12",
54 F13, "F13",
55 F14, "F14",
56 F15, "F15",
57 F16, "F16",
58 F17, "F17",
59 F18, "F18",
60 F19, "F19",
61 F20, "F20",
62 F21, "F21",
63 F22, "F22",
64 F23, "F23",
65 F24, "F24",
66 Home, "Home",
67 MetaLeft, "MetaLeft", PageDown, "PageDown",
69 PageUp, "PageUp",
70 Return, "Enter",
71 ShiftLeft, "ShiftLeft",
72 ShiftRight, "ShiftRight",
73 Space, "Space",
74 Tab, "Tab",
75 PrintScreen, "PrintScreen",
76 ScrollLock, "ScrollLock",
77 NumLock, "NumLock", BackQuote, "Backquote",
79 Num1, "Digit1",
80 Num2, "Digit2",
81 Num3, "Digit3",
82 Num4, "Digit4",
83 Num5, "Digit5",
84 Num6, "Digit6",
85 Num7, "Digit7",
86 Num8, "Digit8",
87 Num9, "Digit9",
88 Num0, "Digit0",
89 Minus, "Minus",
90 Equal, "Equal",
91 KeyQ, "KeyQ",
92 KeyW, "KeyW",
93 KeyE, "KeyE",
94 KeyR, "KeyR",
95 KeyT, "KeyT",
96 KeyY, "KeyY",
97 KeyU, "KeyU",
98 KeyI, "KeyI",
99 KeyO, "KeyO",
100 KeyP, "KeyP",
101 LeftBracket, "BracketLeft",
102 RightBracket, "BracketRight",
103 BackSlash, "Backslash",
104 KeyA, "KeyA",
105 KeyS, "KeyS",
106 KeyD, "KeyD",
107 KeyF, "KeyF",
108 KeyG, "KeyG",
109 KeyH, "KeyH",
110 KeyJ, "KeyJ",
111 KeyK, "KeyK",
112 KeyL, "KeyL",
113 SemiColon, "Semicolon",
114 Quote, "Quote",
115 IntlBackslash, "IntlBackslash",
116 IntlRo, "IntlRo", IntlYen, "IntlYen",
118 KanaMode, "KanaMode", KeyZ, "KeyZ",
120 KeyX, "KeyX",
121 KeyC, "KeyC",
122 KeyV, "KeyV",
123 KeyB, "KeyB",
124 KeyN, "KeyN",
125 KeyM, "KeyM",
126 Comma, "Comma",
127 Dot, "Period",
128 Slash, "Slash",
129 Insert, "Insert",
130 KpMinus, "NumpadSubtract",
131 KpPlus, "NumpadAdd",
132 KpMultiply, "NumpadMultiply",
133 KpDivide, "NumpadDivide",
134 KpDecimal, "NumpadDecimal",
135 KpReturn, "NumpadEnter",
136 KpEqual, "NumpadEqual", KpComma, "NumpadComma", Kp0, "Numpad0",
139 Kp1, "Numpad1",
140 Kp2, "Numpad2",
141 Kp3, "Numpad3",
142 Kp4, "Numpad4",
143 Kp5, "Numpad5",
144 Kp6, "Numpad6",
145 Kp7, "Numpad7",
146 Kp8, "Numpad8",
147 Kp9, "Numpad9",
148 MetaRight, "MetaRight", Apps, "ContextMenu",
150 VolumeUp, "AudioVolumeUp", VolumeDown, "AudioVolumeDown", VolumeMute, "AudioVolumeMute", Lang1, "NonConvert", Lang2, "Convert", Lang3, "Lang3", Lang4, "Lang4", Lang5, "Lang5", Cancel, "",
159 Clear, "",
160 Kana, "",
161 Junja, "",
162 Final, "",
163 Hanja, "",
164 Select, "",
165 Print, "",
166 Execute, "",
167 Help, "",
168 Sleep, "",
169 Separator, "",
170 Pause, ""
171}
172
173#[cfg(test)]
174mod test {
175 use super::{code_from_key, key_from_code};
176 #[test]
177 fn test_reversible() {
178 for code in ["KeyA", "KeyB", "KeyC"] {
179 let key = key_from_code(code);
180 if let Some(code2) = code_from_key(key) {
181 assert_eq!(code, code2)
182 } else {
183 assert!(false, "We could not convert back code: {:?}", code);
184 }
185 }
186 }
187}