#![warn(missing_docs)]
use winit::keyboard::{Key, NativeKey, NativeKeyCode, PhysicalKey};
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_arch = "wasm32")]
mod web;
#[cfg(windows)]
mod windows;
#[cfg(target_os = "linux")]
use linux as os;
#[cfg(target_os = "macos")]
use macos as os;
#[cfg(target_arch = "wasm32")]
use web as os;
#[cfg(windows)]
use windows as os;
pub const MODIFIERS_ORDER: &str = os::MODIFIERS_ORDER;
pub const CTRL_STR: &str = "Ctrl";
pub const SHIFT_STR: &str = "Shift";
pub const ALT_STR: &str = os::ALT_STR;
pub const LOGO_STR: &str = os::LOGO_STR;
pub fn mods_prefix_string(shift: bool, ctrl: bool, alt: bool, logo: bool) -> String {
let mut ret = String::new();
for ch in MODIFIERS_ORDER.chars() {
match ch {
's' if shift => {
ret += SHIFT_STR;
ret += " + ";
}
'c' if ctrl => {
ret += CTRL_STR;
ret += " + ";
}
'a' if alt => {
ret += ALT_STR;
ret += " + ";
}
'm' if logo => {
ret += LOGO_STR;
ret += " + ";
}
_ => (),
}
}
ret
}
pub fn physical_key_name(physical_key: PhysicalKey) -> String {
os::try_physical_key_name(physical_key).unwrap_or_else(|| match physical_key {
PhysicalKey::Code(key_code) => format!("{key_code:?}"),
PhysicalKey::Unidentified(native_key_code) => match native_key_code {
NativeKeyCode::Unidentified => "<unknown>".to_string(),
NativeKeyCode::Android(sc) => format!("SC{sc}"),
NativeKeyCode::MacOS(sc) => format!("SC{sc}"),
NativeKeyCode::Windows(sc) => format!("SC{sc}"),
NativeKeyCode::Xkb(sc) => format!("SC{sc}"),
},
})
}
pub fn key_name(key: Key) -> String {
match key {
Key::Named(named_key) => match os::os_specific_key_name(named_key) {
Some(name) => name.to_string(),
None => format!("{named_key:?}"),
},
Key::Character(c) => c.to_ascii_uppercase(),
Key::Unidentified(native_key) => match native_key {
NativeKey::Unidentified => "<unknown>".to_string(),
NativeKey::Android(sc) => format!("SC{sc}"),
NativeKey::MacOS(sc) => format!("SC{sc}"),
NativeKey::Windows(sc) => format!("SC{sc}"),
NativeKey::Xkb(sc) => format!("SC{sc}"),
NativeKey::Web(smol_str) => format!("{smol_str}"),
},
Key::Dead(None) => "<unknown>".to_string(),
Key::Dead(Some(c)) => c.into(),
}
}