use crate::rdev::Key;
pub const RESERVED_UNKNOWN_CODE: u32 = 0;
macro_rules! decl_keycodes {
($($key:ident, $code:expr),*) => {
pub fn code_from_key(key: Key) -> Option<&'static str> {
match key {
$(
Key::$key => Some($code),
)*
_ => None,
}
}
pub fn key_from_code(code: &str) -> Key {
match code {
$(
$code => Key::$key,
)*
_ => Key::Unknown(RESERVED_UNKNOWN_CODE)
}
}
};
}
decl_keycodes! {
Alt, "AltLeft",
AltGr, "AltRight",
Backspace, "Backspace",
CapsLock, "CapsLock",
ControlLeft, "ControlLeft",
ControlRight, "ControlRight",
Delete, "Delete",
UpArrow, "ArrowUp",
DownArrow, "ArrowDown",
LeftArrow, "ArrowLeft",
RightArrow, "ArrowRight",
End, "End",
Escape, "Escape",
F1, "F1",
F2, "F2",
F3, "F3",
F4, "F4",
F5, "F5",
F6, "F6",
F7, "F7",
F8, "F8",
F9, "F9",
F10, "F10",
F11, "F11",
F12, "F12",
F13, "F13",
F14, "F14",
F15, "F15",
F16, "F16",
F17, "F17",
F18, "F18",
F19, "F19",
F20, "F20",
F21, "F21",
F22, "F22",
F23, "F23",
F24, "F24",
Home, "Home",
MetaLeft, "MetaLeft", PageDown, "PageDown",
PageUp, "PageUp",
Return, "Enter",
ShiftLeft, "ShiftLeft",
ShiftRight, "ShiftRight",
Space, "Space",
Tab, "Tab",
PrintScreen, "PrintScreen",
ScrollLock, "ScrollLock",
NumLock, "NumLock", BackQuote, "Backquote",
Num1, "Digit1",
Num2, "Digit2",
Num3, "Digit3",
Num4, "Digit4",
Num5, "Digit5",
Num6, "Digit6",
Num7, "Digit7",
Num8, "Digit8",
Num9, "Digit9",
Num0, "Digit0",
Minus, "Minus",
Equal, "Equal",
KeyQ, "KeyQ",
KeyW, "KeyW",
KeyE, "KeyE",
KeyR, "KeyR",
KeyT, "KeyT",
KeyY, "KeyY",
KeyU, "KeyU",
KeyI, "KeyI",
KeyO, "KeyO",
KeyP, "KeyP",
LeftBracket, "BracketLeft",
RightBracket, "BracketRight",
BackSlash, "Backslash",
KeyA, "KeyA",
KeyS, "KeyS",
KeyD, "KeyD",
KeyF, "KeyF",
KeyG, "KeyG",
KeyH, "KeyH",
KeyJ, "KeyJ",
KeyK, "KeyK",
KeyL, "KeyL",
SemiColon, "Semicolon",
Quote, "Quote",
IntlBackslash, "IntlBackslash",
IntlRo, "IntlRo", IntlYen, "IntlYen",
KanaMode, "KanaMode", KeyZ, "KeyZ",
KeyX, "KeyX",
KeyC, "KeyC",
KeyV, "KeyV",
KeyB, "KeyB",
KeyN, "KeyN",
KeyM, "KeyM",
Comma, "Comma",
Dot, "Period",
Slash, "Slash",
Insert, "Insert",
KpMinus, "NumpadSubtract",
KpPlus, "NumpadAdd",
KpMultiply, "NumpadMultiply",
KpDivide, "NumpadDivide",
KpDecimal, "NumpadDecimal",
KpReturn, "NumpadEnter",
KpEqual, "NumpadEqual", KpComma, "NumpadComma", Kp0, "Numpad0",
Kp1, "Numpad1",
Kp2, "Numpad2",
Kp3, "Numpad3",
Kp4, "Numpad4",
Kp5, "Numpad5",
Kp6, "Numpad6",
Kp7, "Numpad7",
Kp8, "Numpad8",
Kp9, "Numpad9",
MetaRight, "MetaRight", Apps, "ContextMenu",
VolumeUp, "AudioVolumeUp", VolumeDown, "AudioVolumeDown", VolumeMute, "AudioVolumeMute", Lang1, "NonConvert", Lang2, "Convert", Lang3, "Lang3", Lang4, "Lang4", Lang5, "Lang5", Cancel, "",
Clear, "",
Kana, "",
Junja, "",
Final, "",
Hanja, "",
Select, "",
Print, "",
Execute, "",
Help, "",
Sleep, "",
Separator, "",
Pause, ""
}
#[cfg(test)]
mod test {
use super::{code_from_key, key_from_code};
#[test]
fn test_reversible() {
for code in ["KeyA", "KeyB", "KeyC"] {
let key = key_from_code(code);
if let Some(code2) = code_from_key(key) {
assert_eq!(code, code2)
} else {
assert!(false, "We could not convert back code: {:?}", code);
}
}
}
}