From 3655f3269e67d1e0b89dc3286b77822ba6cc44a3 Mon Sep 17 00:00:00 2001 From: Joe Bellus Date: Sat, 13 Apr 2024 00:22:40 -0400 Subject: [PATCH] Added handling for modifier keys Modifier key detection is now available in the keyboard resource --- src/app.rs | 1 + src/input.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index f279266..68b9d20 100644 --- a/src/app.rs +++ b/src/app.rs @@ -190,6 +190,7 @@ where let container = self.container.borrow(); let kb = container.get::>().unwrap(); kb.set_key(key_event.code); + kb.set_modifiers(key_event.modifiers); } Event::Mouse(_) => todo!(), Event::Paste(_) => todo!(), diff --git a/src/input.rs b/src/input.rs index 50ff0f9..6810db3 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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>>, + modifiers: Rc>, +} +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) + } }