mirror of https://github.com/k0kubun/xremap
Implement key parser
parent
d9127e8a90
commit
2ee85b8773
@ -0,0 +1,34 @@
|
||||
use evdev::Key;
|
||||
use std::error::Error;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub fn parse_key(input: &str) -> Result<Key, Box<dyn Error>> {
|
||||
// Everything is case-insensitive
|
||||
let name = input.to_uppercase();
|
||||
|
||||
// Original evdev scancodes should always work
|
||||
if let Ok(key) = Key::from_str(&name) {
|
||||
return Ok(key);
|
||||
}
|
||||
|
||||
// You can abbreviate "KEY_" of any "KEY_*" scancodes.
|
||||
if let Ok(key) = Key::from_str(&format!("KEY_{}", name)) {
|
||||
return Ok(key);
|
||||
}
|
||||
|
||||
// xremap's custom aliases like k0kubun/karabiner-dsl
|
||||
let key = match &name[..] {
|
||||
"CTRL_R" => Key::KEY_RIGHTCTRL,
|
||||
"CTRL_L" => Key::KEY_LEFTCTRL,
|
||||
"SHIFT_R" => Key::KEY_RIGHTSHIFT,
|
||||
"SHIFT_L" => Key::KEY_LEFTSHIFT,
|
||||
"ALT_R" => Key::KEY_RIGHTALT,
|
||||
"ALT_L" => Key::KEY_LEFTALT,
|
||||
_ => Key::KEY_RESERVED,
|
||||
};
|
||||
if key != Key::KEY_RESERVED {
|
||||
return Ok(key);
|
||||
}
|
||||
|
||||
return Err(format!("unknown key '{}'", input).into());
|
||||
}
|
Loading…
Reference in New Issue