Skip to content
Snippets Groups Projects
Commit 14b598be authored by MrGVSV's avatar MrGVSV
Browse files

Added KeyboardEvent and KeyboardModifiers

These allow us to pass along metadata with a keyboard event
(specifically here, which modifier keys are active)
parent 02aad4d4
No related branches found
No related tags found
No related merge requests found
use crate::{Index, KeyCode}; use crate::{Index, KeyboardEvent};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Event { pub struct Event {
...@@ -59,8 +59,8 @@ pub enum EventType { ...@@ -59,8 +59,8 @@ pub enum EventType {
Focus, Focus,
Blur, Blur,
CharInput { c: char }, CharInput { c: char },
KeyUp { key: KeyCode }, KeyUp(KeyboardEvent),
KeyDown { key: KeyCode }, KeyDown(KeyboardEvent),
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
...@@ -83,8 +83,8 @@ impl EventType { ...@@ -83,8 +83,8 @@ impl EventType {
Self::MouseDown => true, Self::MouseDown => true,
Self::MouseUp => true, Self::MouseUp => true,
Self::CharInput { .. } => true, Self::CharInput { .. } => true,
Self::KeyUp { .. } => true, Self::KeyUp(..) => true,
Self::KeyDown { .. } => true, Self::KeyDown(..) => true,
// Doesn't Propagate // Doesn't Propagate
Self::MouseIn => false, Self::MouseIn => false,
Self::MouseOut => false, Self::MouseOut => false,
...@@ -105,11 +105,11 @@ impl EventType { ...@@ -105,11 +105,11 @@ impl EventType {
Self::MouseOut => EventCategory::Mouse, Self::MouseOut => EventCategory::Mouse,
// Keyboard // Keyboard
Self::CharInput { .. } => EventCategory::Keyboard, Self::CharInput { .. } => EventCategory::Keyboard,
Self::KeyUp { .. } => EventCategory::Keyboard, Self::KeyUp(..) => EventCategory::Keyboard,
Self::KeyDown { .. } => EventCategory::Keyboard, Self::KeyDown(..) => EventCategory::Keyboard,
// Focus // Focus
Self::Focus => EventCategory::Focus, Self::Focus => EventCategory::Focus,
Self::Blur => EventCategory::Focus, Self::Blur => EventCategory::Focus,
} }
} }
} }
\ No newline at end of file
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use crate::{Event, EventType, Index, InputEvent, InputEventCategory, KayakContext, PointerEvents}; use crate::{Event, EventType, Index, InputEvent, InputEventCategory, KayakContext, KeyboardEvent, KeyboardModifiers, KeyCode, PointerEvents};
use crate::layout_cache::Rect; use crate::layout_cache::Rect;
use crate::widget_manager::WidgetManager; use crate::widget_manager::WidgetManager;
...@@ -35,6 +35,7 @@ pub(crate) struct EventDispatcher { ...@@ -35,6 +35,7 @@ pub(crate) struct EventDispatcher {
current_mouse_position: (f32, f32), current_mouse_position: (f32, f32),
next_mouse_position: (f32, f32), next_mouse_position: (f32, f32),
previous_events: EventMap, previous_events: EventMap,
keyboard_modifiers: KeyboardModifiers,
} }
impl EventDispatcher { impl EventDispatcher {
...@@ -303,14 +304,26 @@ impl EventDispatcher { ...@@ -303,14 +304,26 @@ impl EventDispatcher {
InputEvent::CharEvent { c } => event_stream.push( InputEvent::CharEvent { c } => event_stream.push(
Event::new(current_focus, EventType::CharInput { c: *c }) Event::new(current_focus, EventType::CharInput { c: *c })
), ),
InputEvent::Keyboard { key, is_pressed } => if *is_pressed { InputEvent::Keyboard { key, is_pressed } => {
event_stream.push( // === Modifers === //
Event::new(current_focus, EventType::KeyDown { key: *key }) match key {
) KeyCode::LControl | KeyCode::RControl => self.keyboard_modifiers.is_ctrl_pressed = *is_pressed,
} else { KeyCode::LShift | KeyCode::RShift => self.keyboard_modifiers.is_shift_pressed = *is_pressed,
event_stream.push( KeyCode::LAlt | KeyCode::RAlt => self.keyboard_modifiers.is_alt_pressed = *is_pressed,
Event::new(current_focus, EventType::KeyUp { key: *key }) KeyCode::LWin | KeyCode::RWin => self.keyboard_modifiers.is_meta_pressed = *is_pressed,
) _ => {}
}
// === Event === //
if *is_pressed {
event_stream.push(
Event::new(current_focus, EventType::KeyDown(KeyboardEvent::new(*key, self.keyboard_modifiers)))
)
} else {
event_stream.push(
Event::new(current_focus, EventType::KeyUp(KeyboardEvent::new(*key, self.keyboard_modifiers)))
)
}
} }
_ => {} _ => {}
} }
......
use crate::KeyCode;
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
pub struct KeyboardModifiers {
/// True if the one of the Control keys is currently pressed
pub is_ctrl_pressed: bool,
/// True if the one of the Shift keys is currently pressed
pub is_shift_pressed: bool,
/// True if the one of the Alt (or "Option") keys is currently pressed
pub is_alt_pressed: bool,
/// True if the one of the Meta keys is currently pressed
///
/// This is the "Command" ("⌘") key on Mac and "Windows" or "Super" on other systems.
pub is_meta_pressed: bool,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct KeyboardEvent {
key: KeyCode,
modifiers: KeyboardModifiers,
}
impl KeyboardEvent {
pub fn new(key: KeyCode, modifiers: KeyboardModifiers) -> Self {
Self {
key,
modifiers,
}
}
/// Returns this event's affected key
pub fn key(&self) -> KeyCode {
self.key
}
/// Returns all modifiers for this event's key
pub fn modifiers(&self) -> KeyboardModifiers {
self.modifiers
}
/// Returns true if the one of the Control keys is currently pressed
pub fn is_ctrl_pressed(&self) -> bool {
self.modifiers.is_ctrl_pressed
}
/// Returns true if the one of the Shift keys is currently pressed
pub fn is_shift_pressed(&self) -> bool {
self.modifiers.is_shift_pressed
}
/// Returns true if the one of the Alt (or "Option") keys is currently pressed
pub fn is_alt_pressed(&self) -> bool {
self.modifiers.is_alt_pressed
}
/// Returns true if the one of the Meta keys is currently pressed
///
/// This is the "Command" ("⌘") key on Mac and "Windows" or "Super" on other systems.
pub fn is_meta_pressed(&self) -> bool {
self.modifiers.is_meta_pressed
}
}
\ No newline at end of file
...@@ -18,6 +18,7 @@ pub mod widget; ...@@ -18,6 +18,7 @@ pub mod widget;
pub mod widget_manager; pub mod widget_manager;
mod cursor; mod cursor;
mod event_dispatcher; mod event_dispatcher;
mod keyboard;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
...@@ -29,11 +30,13 @@ pub use event::*; ...@@ -29,11 +30,13 @@ pub use event::*;
pub use fragment::Fragment; pub use fragment::Fragment;
pub use generational_arena::{Arena, Index}; pub use generational_arena::{Arena, Index};
pub use input_event::*; pub use input_event::*;
pub use keyboard::{KeyboardEvent, KeyboardModifiers};
pub use keys::KeyCode; pub use keys::KeyCode;
pub use resources::Resources; pub use resources::Resources;
pub use tree::{Tree, WidgetTree}; pub use tree::{Tree, WidgetTree};
pub use vec::VecTracker; pub use vec::VecTracker;
pub use widget::Widget; pub use widget::Widget;
pub mod derivative { pub mod derivative {
pub use derivative::*; pub use derivative::*;
} }
...@@ -44,7 +47,7 @@ pub type Children = Option< ...@@ -44,7 +47,7 @@ pub type Children = Option<
#[derive(Clone)] #[derive(Clone)]
pub struct OnEvent( pub struct OnEvent(
pub Arc< pub Arc<
RwLock<dyn FnMut(&mut crate::context::KayakContext, &mut Event) + Send + Sync + 'static>, RwLock<dyn FnMut(&mut crate::context::KayakContext, &mut Event) + Send + Sync + 'static>,
>, >,
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment