rdev/linux/
keycodes.rs

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