Skip to main content

rdevin/keycodes/
chrome.rs

1use crate::rdevin::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            #[allow(unreachable_patterns)]
18            match code {
19                $(
20                    $code => Key::$key,
21                )*
22                _ => Key::Unknown(RESERVED_UNKNOWN_CODE)
23            }
24        }
25    };
26}
27
28// https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values
29decl_keycodes! {
30    Alt, "AltLeft",
31    AltGr, "AltRight",
32    Backspace, "Backspace",
33    CapsLock, "CapsLock",
34    ControlLeft, "ControlLeft",
35    ControlRight, "ControlRight",
36    Delete, "Delete",
37    UpArrow, "ArrowUp",
38    DownArrow, "ArrowDown",
39    LeftArrow, "ArrowLeft",
40    RightArrow, "ArrowRight",
41    End, "End",
42    Escape, "Escape",
43    F1, "F1",
44    F2, "F2",
45    F3, "F3",
46    F4, "F4",
47    F5, "F5",
48    F6, "F6",
49    F7, "F7",
50    F8, "F8",
51    F9, "F9",
52    F10, "F10",
53    F11, "F11",
54    F12, "F12",
55    F13, "F13",
56    F14, "F14",
57    F15, "F15",
58    F16, "F16",
59    F17, "F17",
60    F18, "F18",
61    F19, "F19",
62    F20, "F20",
63    F21, "F21",
64    F22, "F22",
65    F23, "F23",
66    F24, "F24",
67    Home, "Home",
68    MetaLeft, "MetaLeft",   // "MetaLeft" (was "OSLeft" prior to Chrome 52)
69    PageDown, "PageDown",
70    PageUp, "PageUp",
71    Return, "Enter",
72    ShiftLeft, "ShiftLeft",
73    ShiftRight, "ShiftRight",
74    Space, "Space",
75    Tab, "Tab",
76    PrintScreen, "PrintScreen",
77    ScrollLock, "ScrollLock",
78    NumLock, "NumLock",            // The scan code is 0x0045, but https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values gives 0xE045
79    BackQuote, "Backquote",
80    Num1, "Digit1",
81    Num2, "Digit2",
82    Num3, "Digit3",
83    Num4, "Digit4",
84    Num5, "Digit5",
85    Num6, "Digit6",
86    Num7, "Digit7",
87    Num8, "Digit8",
88    Num9, "Digit9",
89    Num0, "Digit0",
90    Minus, "Minus",
91    Equal, "Equal",
92    KeyQ, "KeyQ",
93    KeyW, "KeyW",
94    KeyE, "KeyE",
95    KeyR, "KeyR",
96    KeyT, "KeyT",
97    KeyY, "KeyY",
98    KeyU, "KeyU",
99    KeyI, "KeyI",
100    KeyO, "KeyO",
101    KeyP, "KeyP",
102    LeftBracket, "BracketLeft",
103    RightBracket, "BracketRight",
104    BackSlash, "Backslash",
105    KeyA, "KeyA",
106    KeyS, "KeyS",
107    KeyD, "KeyD",
108    KeyF, "KeyF",
109    KeyG, "KeyG",
110    KeyH, "KeyH",
111    KeyJ, "KeyJ",
112    KeyK, "KeyK",
113    KeyL, "KeyL",
114    SemiColon, "Semicolon",
115    Quote, "Quote",
116    IntlBackslash, "IntlBackslash",
117    IntlRo, "IntlRo", // "IntlRo" (was "" prior to Chrome 48)
118    IntlYen, "IntlYen",
119    KanaMode, "KanaMode", // "KanaMode" (was "" prior to Chrome 48)
120    KeyZ, "KeyZ",
121    KeyX, "KeyX",
122    KeyC, "KeyC",
123    KeyV, "KeyV",
124    KeyB, "KeyB",
125    KeyN, "KeyN",
126    KeyM, "KeyM",
127    Comma, "Comma",
128    Dot, "Period",
129    Slash, "Slash",
130    Insert, "Insert",
131    KpMinus, "NumpadSubtract",
132    KpPlus, "NumpadAdd",
133    KpMultiply, "NumpadMultiply",
134    KpDivide, "NumpadDivide",
135    KpDecimal, "NumpadDecimal",
136    KpReturn, "NumpadEnter",
137    KpEqual, "NumpadEqual", // 	"NumpadEqual" (was "" prior to Chrome 48)
138    KpComma, "NumpadComma", // "NumpadComma" (was "" prior to Chrome 48)
139    Kp0, "Numpad0",
140    Kp1, "Numpad1",
141    Kp2, "Numpad2",
142    Kp3, "Numpad3",
143    Kp4, "Numpad4",
144    Kp5, "Numpad5",
145    Kp6, "Numpad6",
146    Kp7, "Numpad7",
147    Kp8, "Numpad8",
148    Kp9, "Numpad9",
149    MetaRight, "MetaRight", // "MetaRight" (was "OSRight" prior to Chrome 52)
150    Apps, "ContextMenu",
151    VolumeUp, "AudioVolumeUp", // "AudioVolumeUp" (was "VolumeUp" prior to Chrome 52) (⚠️ Not the same on Firefox)
152    VolumeDown, "AudioVolumeDown", // "AudioVolumeDown" (was "VolumeDown" prior to Chrome 52) (⚠️ Not the same on Firefox)
153    VolumeMute, "AudioVolumeMute", // "AudioVolumeMute" (was "VolumeMute" prior to Chrome 52) (⚠️ Not the same on Firefox)
154    Lang1, "NonConvert", // "NonConvert" (was "" prior to Chrome 48)
155    Lang2, "Convert", // "Convert" (was "" prior to Chrome 48)
156    Lang3, "Lang3", // "Lang3" (was "" prior to Chrome 48)
157    Lang4, "Lang4", // "Lang4" (was "" prior to Chrome 48)
158    Lang5, "Lang5", // "Lang5" (was "" prior to Chrome 48) (⚠️ Not the same on Firefox) Lang5 in https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf is 0x0075, while is "Lang5" in https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code is 0x005D
159    Cancel, "",
160    Clear, "",
161    Kana, "",
162    Junja, "",
163    Final, "",
164    Hanja, "",
165    Select, "",
166    Print, "",
167    Execute, "",
168    Help, "",
169    Sleep, "",
170    Separator, "",
171    Pause, ""
172}
173
174#[cfg(test)]
175mod test {
176    use super::{code_from_key, key_from_code};
177    #[test]
178    fn test_reversible() {
179        for code in ["KeyA", "KeyB", "KeyC"] {
180            let key = key_from_code(code);
181            if let Some(code2) = code_from_key(key) {
182                assert_eq!(code, code2)
183            } else {
184                panic!("We could not convert back code: {:?}", code);
185            }
186        }
187    }
188}