mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2024-12-23 11:21:30 +11:00
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 <sean@xobs.io> * fix: EventQueue::dispatch blocks until some events are available (#265) use non blocking alternative as documented in EventQueue Co-authored-by: vemoo <berublan@gmail.com>
This commit is contained in:
parent
8927b29a69
commit
8b9445eea4
|
@ -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<Key> {
|
||||
|
|
11
src/lib.rs
11
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;
|
||||
|
|
Loading…
Reference in a new issue