1use crate::rdev::Key;
2
3macro_rules! decl_keycodes {
4 ($($key:ident, $code:literal),*) => {
5 pub fn code_from_key(key: Key) -> Option<u32> {
7 match key {
8 $(
9 Key::$key => Some($code),
10 )*
11 Key::Unknown(code) => Some(code),
12 _ => None,
13 }
14 }
15
16 #[allow(dead_code)]
18 pub fn key_from_code(code: u32) -> Key {
19 match code {
20 $(
21 $code => Key::$key,
22 )*
23 _ => Key::Unknown(code)
24 }
25 }
26 };
27}
28
29#[rustfmt::skip]
30decl_keycodes!(
31 Alt, 64,
32 AltGr, 108,
33 Backspace, 22,
34 CapsLock, 66,
35 ControlLeft, 37,
36 ControlRight, 105,
37 Delete, 119,
38 DownArrow, 116,
39 End, 115,
40 Escape, 9,
41 F1, 67,
42 F10, 76,
43 F11, 95,
44 F12, 96,
45 F13, 0xBF,
46 F14, 0xC0,
47 F15, 0xC1,
48 F16, 0xC2,
49 F17, 0xC3,
50 F18, 0xC4,
51 F19, 0xC5,
52 F20, 0xC6,
53 F21, 0xC7,
54 F22, 0xC8,
55 F23, 0xC9,
56 F24, 0xCA,
57 F2, 68,
58 F3, 69,
59 F4, 70,
60 F5, 71,
61 F6, 72,
62 F7, 73,
63 F8, 74,
64 F9, 75,
65 Home, 110,
66 LeftArrow, 113,
67 MetaLeft, 133,
68 PageDown, 117,
69 PageUp, 112,
70 Return, 36,
71 RightArrow, 114,
72 ShiftLeft, 50,
73 ShiftRight, 62,
74 Space, 65,
75 Tab, 23,
76 UpArrow, 111,
77 PrintScreen, 107,
78 ScrollLock, 78,
79 Pause, 127,
80 NumLock, 77,
81 BackQuote, 49,
82 Num1, 10,
83 Num2, 11,
84 Num3, 12,
85 Num4, 13,
86 Num5, 14,
87 Num6, 15,
88 Num7, 16,
89 Num8, 17,
90 Num9, 18,
91 Num0, 19,
92 Minus, 20,
93 Equal, 21,
94 KeyQ, 24,
95 KeyW, 25,
96 KeyE, 26,
97 KeyR, 27,
98 KeyT, 28,
99 KeyY, 29,
100 KeyU, 30,
101 KeyI, 31,
102 KeyO, 32,
103 KeyP, 33,
104 LeftBracket, 34,
105 RightBracket, 35,
106 KeyA, 38,
107 KeyS, 39,
108 KeyD, 40,
109 KeyF, 41,
110 KeyG, 42,
111 KeyH, 43,
112 KeyJ, 44,
113 KeyK, 45,
114 KeyL, 46,
115 SemiColon, 47,
116 Quote, 48,
117 BackSlash, 51,
118 IntlBackslash, 94,
119 IntlRo, 0x61,
120 IntlYen, 0x84,
121 KanaMode, 0x65,
122 KeyZ, 52,
123 KeyX, 53,
124 KeyC, 54,
125 KeyV, 55,
126 KeyB, 56,
127 KeyN, 57,
128 KeyM, 58,
129 Comma, 59,
130 Dot, 60,
131 Slash, 61,
132 Insert, 118,
133 KpDecimal, 91,
134 KpReturn, 104,
135 KpMinus, 82,
136 KpPlus, 86,
137 KpMultiply, 63,
138 KpDivide, 106,
139 KpEqual, 0x7D,
140 KpComma, 0x81,
141 Kp0, 90,
142 Kp1, 87,
143 Kp2, 88,
144 Kp3, 89,
145 Kp4, 83,
146 Kp5, 84,
147 Kp6, 85,
148 Kp7, 79,
149 Kp8, 80,
150 Kp9, 81,
151 MetaRight, 134,
152 Apps, 135,
153 VolumeUp, 0x007B,
154 VolumeDown, 0x007A,
155 VolumeMute, 0x0079,
156 Lang1, 0x0066,
157 Lang2, 0x0064,
158 Lang3, 0x0062,
159 Lang4, 0x0063,
160 Lang5, 0x005d
161);
162
163#[cfg(test)]
164mod test {
165 use super::{code_from_key, key_from_code};
166 #[test]
167 fn test_reversible() {
168 for code in 0..65636 {
169 let key = key_from_code(code);
170 match code_from_key(key) {
171 Some(code2) => assert_eq!(code, code2),
172 None => panic!("Could not convert back code: {:?}", code),
173 }
174 }
175 }
176}