From 8b9445eea4b8307afe5dcc727f9bb3553b611579 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 26 Jan 2022 15:23:38 +0800 Subject: [PATCH] key_handler: add a callback for key events (#243) * key_handler: add a callback for key events Add a callback for key events. This includes complete events such as Shift, not just text events. A default handler is provided for backwards compatibility. Signed-off-by: Sean Cross * fix: EventQueue::dispatch blocks until some events are available (#265) use non blocking alternative as documented in EventQueue Co-authored-by: vemoo --- src/key_handler.rs | 3 +++ src/lib.rs | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/key_handler.rs b/src/key_handler.rs index acf63ba..da5ccc7 100644 --- a/src/key_handler.rs +++ b/src/key_handler.rs @@ -30,6 +30,9 @@ impl KeyHandler { #[inline] pub fn set_key_state(&mut self, key: Key, state: bool) { self.keys[key as usize] = state; + if let Some(cb) = &mut self.key_callback { + cb.set_key_state(key, state); + } } pub fn get_keys(&self) -> Vec { diff --git a/src/lib.rs b/src/lib.rs index ac4e3e9..7e6304a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,10 +83,17 @@ pub enum CursorStyle { ResizeAll, } -/// This trait can be implemented and set with ```set_input_callback``` to reieve a callback -/// whene there is inputs incoming. Currently only support unicode chars. +/// This trait can be implemented and set with ```set_input_callback``` to receive a callback +/// when there is inputs. pub trait InputCallback { + /// Called when text is added to the window, or a key is pressed. This passes + /// in a unicode character, and therefore does not report control characters. fn add_char(&mut self, uni_char: u32); + + /// Called whenever a key is pressed or released. This reports the state of the + /// key in the `state` argument, as well as the translated key in the `key` argument. + /// This includes control characters such as `Key::LeftShift`. + fn set_key_state(&mut self, _key: Key, _state: bool) {} } mod error;