#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Key {
Char(char),
Enter,
Escape,
Tab,
BackTab,
Backspace,
Delete,
Up,
Down,
Left,
Right,
Home,
End,
PageUp,
PageDown,
F(u8),
Insert,
Null,
Unknown,
}
impl Key {
pub fn ctrl(ch: char) -> KeyBinding {
KeyBinding {
key: Key::Char(ch),
ctrl: true,
alt: false,
shift: false,
}
}
pub fn alt(ch: char) -> KeyBinding {
KeyBinding {
key: Key::Char(ch),
ctrl: false,
alt: true,
shift: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeyBinding {
pub key: Key,
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
}
pub struct KeyMap<A> {
bindings: std::collections::HashMap<KeyBinding, A>,
}
impl<A: Clone> KeyMap<A> {
pub fn new() -> Self {
Self {
bindings: std::collections::HashMap::new(),
}
}
pub fn bind(&mut self, binding: KeyBinding, action: A) {
self.bindings.insert(binding, action);
}
pub fn get(&self, binding: &KeyBinding) -> Option<&A> {
self.bindings.get(binding)
}
}
impl<A: Clone> Default for KeyMap<A> {
fn default() -> Self {
Self::new()
}
}