1use crate::rdev::Key;
2
3macro_rules! decl_keycodes {
4 ($($key:ident, $code:literal),*) => {
5 #[allow(dead_code)]
7 pub fn code_from_key(key: Key) -> Option<u32> {
8 match key {
9 $(
10 Key::$key => Some($code),
11 )*
12 Key::Unknown(code) => Some(code),
13 _ => None,
14 }
15 }
16
17 #[allow(dead_code)]
19 pub fn key_from_code(code: u32) -> Key {
20 match code {
21 $(
22 $code => Key::$key,
23 )*
24 _ => Key::Unknown(code)
25 }
26 }
27 };
28}
29
30#[rustfmt::skip]
31decl_keycodes!(
32 Alt, 57,
33 AltGr, 58,
34 Backspace, 67,
35 CapsLock, 115,
36 ControlLeft, 113,
37 ControlRight, 114,
38 Delete, 112,
39 DownArrow, 20,
40 End, 123,
41 Escape, 111,
42 F1, 131,
43 F10, 140,
44 F11, 141,
45 F12, 142,
46 F2, 132,
47 F3, 133,
48 F4, 134,
49 F5, 135,
50 F6, 136,
51 F7, 137,
52 F8, 138,
53 F9, 139,
54 Home, 3,
55 LeftArrow, 21,
56 MetaLeft, 117,
57 PageDown, 93,
58 PageUp, 92,
59 Return, 66,
60 RightArrow, 22,
61 ShiftLeft, 59,
62 ShiftRight, 60,
63 Space, 62,
64 Tab, 61,
65 UpArrow, 19,
66 PrintScreen, 120,
67 ScrollLock, 116,
68 NumLock, 143,
69 Pause, 121,
70 BackQuote, 75,
71 Num1, 8,
72 Num2, 9,
73 Num3, 10,
74 Num4, 11,
75 Num5, 12,
76 Num6, 13,
77 Num7, 14,
78 Num8, 15,
79 Num9, 16,
80 Num0, 7,
81 Minus, 69,
82 Equal, 70,
83 KeyA, 29,
84 KeyB, 30,
85 KeyC, 31,
86 KeyD, 32,
87 KeyE, 33,
88 KeyF, 34,
89 KeyG, 35,
90 KeyH, 36,
91 KeyI, 37,
92 KeyJ, 38,
93 KeyK, 39,
94 KeyL, 40,
95 KeyM, 41,
96 KeyN, 42,
97 KeyO, 43,
98 KeyP, 44,
99 KeyQ, 45,
100 KeyR, 46,
101 KeyS, 47,
102 KeyT, 48,
103 KeyU, 49,
104 KeyV, 50,
105 KeyW, 51,
106 KeyX, 52,
107 KeyY, 53,
108 KeyZ, 54,
109 LeftBracket, 71,
110 RightBracket, 72,
111
112 SemiColon, 74,
113 Quote, 75,
114 BackSlash, 73,
115 KanaMode, 218,
116
117 Comma, 55,
118 Dot, 56,
119 Slash, 76,
120 Insert, 124
121);
122
123#[cfg(test)]
124mod test {
125 use super::{code_from_key, key_from_code};
126 #[test]
127 fn test_reversible() {
128 for code in 0..65636 {
129 let key = key_from_code(code);
130 match code_from_key(key) {
131 Some(code2) => assert_eq!(code, code2),
132 None => panic!("Could not convert back code: {:?}", code),
133 }
134 }
135 }
136}