Added handling for modifier keys

Modifier key detection is now available in the keyboard resource
This commit is contained in:
Joe Bellus 2024-04-13 00:22:40 -04:00
parent 99137d1708
commit 3655f3269e
2 changed files with 40 additions and 2 deletions

View File

@ -190,6 +190,7 @@ where
let container = self.container.borrow();
let kb = container.get::<Res<Keyboard>>().unwrap();
kb.set_key(key_event.code);
kb.set_modifiers(key_event.modifiers);
}
Event::Mouse(_) => todo!(),
Event::Paste(_) => todo!(),

View File

@ -1,13 +1,22 @@
use std::{cell::RefCell, rc::Rc};
use crossterm::event::KeyCode;
use crossterm::event::{KeyCode, KeyModifiers, ModifierKeyCode};
/// Keyboard can be used as an injectable resource that provides information
/// about the current keyboard state. This is the primary mechanism by which
/// applications can respond to keyboard input from users.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Keyboard {
key: Rc<RefCell<Option<KeyCode>>>,
modifiers: Rc<RefCell<KeyModifiers>>,
}
impl Default for Keyboard {
fn default() -> Self {
Self {
key: Rc::new(RefCell::new(None)),
modifiers: Rc::new(RefCell::new(KeyModifiers::empty())),
}
}
}
impl Keyboard {
@ -19,6 +28,10 @@ impl Keyboard {
*self.key.borrow_mut() = Some(k);
}
pub fn set_modifiers(&self, modifiers: KeyModifiers) {
*self.modifiers.borrow_mut() = modifiers;
}
pub fn reset(&self) {
*self.key.borrow_mut() = None;
}
@ -34,4 +47,28 @@ impl Keyboard {
None
}
}
pub fn shift(&self) -> bool {
self.modifiers.borrow().contains(KeyModifiers::SHIFT)
}
pub fn control(&self) -> bool {
self.modifiers.borrow().contains(KeyModifiers::CONTROL)
}
pub fn alt(&self) -> bool {
self.modifiers.borrow().contains(KeyModifiers::ALT)
}
pub fn super_key(&self) -> bool {
self.modifiers.borrow().contains(KeyModifiers::SUPER)
}
pub fn hyper(&self) -> bool {
self.modifiers.borrow().contains(KeyModifiers::HYPER)
}
pub fn meta(&self) -> bool {
self.modifiers.borrow().contains(KeyModifiers::META)
}
}