Split Input keyboard event by ButtonState

This commit is contained in:
Héctor Ramón Jiménez 2020-04-30 04:34:29 +02:00
parent af95d3972e
commit e139aae143
4 changed files with 44 additions and 28 deletions

View File

@ -1,5 +1,4 @@
use super::{KeyCode, ModifiersState}; use super::{KeyCode, ModifiersState};
use crate::input::ButtonState;
/// A keyboard event. /// A keyboard event.
/// ///
@ -9,11 +8,17 @@ use crate::input::ButtonState;
/// [open an issue]: https://github.com/hecrj/iced/issues /// [open an issue]: https://github.com/hecrj/iced/issues
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum Event { pub enum Event {
/// A keyboard key was pressed or released. /// A keyboard key was pressed.
Input { KeyPressed {
/// The state of the key /// The key identifier
state: ButtonState, key_code: KeyCode,
/// The state of the modifier keys
modifiers: ModifiersState,
},
/// A keyboard key was released.
KeyReleased {
/// The key identifier /// The key identifier
key_code: KeyCode, key_code: KeyCode,

View File

@ -532,24 +532,19 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. }) => { Event::Mouse(mouse::Event::CursorMoved { .. }) => {
self.trigger_resize(layout, cursor_position, messages); self.trigger_resize(layout, cursor_position, messages);
} }
Event::Keyboard(keyboard::Event::Input { Event::Keyboard(keyboard::Event::KeyPressed {
modifiers, modifiers,
key_code, key_code,
state,
}) => { }) => {
if let Some(on_key_press) = &self.on_key_press { if let Some(on_key_press) = &self.on_key_press {
// TODO: Discard when event is captured // TODO: Discard when event is captured
if state == ButtonState::Pressed { if let Some(_) = self.state.active_pane() {
if let Some(_) = self.state.active_pane() { if modifiers.matches(self.modifier_keys) {
if modifiers.matches(self.modifier_keys) { if let Some(message) = on_key_press(KeyPressEvent {
if let Some(message) = key_code,
on_key_press(KeyPressEvent { modifiers,
key_code, }) {
modifiers, messages.push(message);
})
{
messages.push(message);
}
} }
} }
} }
@ -557,6 +552,11 @@ where
*self.pressed_modifiers = modifiers; *self.pressed_modifiers = modifiers;
} }
Event::Keyboard(keyboard::Event::KeyReleased {
modifiers, ..
}) => {
*self.pressed_modifiers = modifiers;
}
_ => {} _ => {}
} }

View File

@ -327,9 +327,8 @@ where
let message = (self.on_change)(editor.contents()); let message = (self.on_change)(editor.contents());
messages.push(message); messages.push(message);
} }
Event::Keyboard(keyboard::Event::Input { Event::Keyboard(keyboard::Event::KeyPressed {
key_code, key_code,
state: ButtonState::Pressed,
modifiers, modifiers,
}) if self.state.is_focused => match key_code { }) if self.state.is_focused => match key_code {
keyboard::KeyCode::Enter => { keyboard::KeyCode::Enter => {
@ -473,10 +472,8 @@ where
} }
_ => {} _ => {}
}, },
Event::Keyboard(keyboard::Event::Input { Event::Keyboard(keyboard::Event::KeyReleased {
key_code, key_code, ..
state: ButtonState::Released,
..
}) => match key_code { }) => match key_code {
keyboard::KeyCode::V => { keyboard::KeyCode::V => {
self.state.is_pasting = None; self.state.is_pasting = None;

View File

@ -70,10 +70,24 @@ pub fn window_event(
.. ..
}, },
.. ..
} => Some(Event::Keyboard(keyboard::Event::Input { } => Some(Event::Keyboard({
key_code: key_code(*virtual_keycode), let key_code = key_code(*virtual_keycode);
state: button_state(*state), let modifiers = modifiers_state(modifiers);
modifiers: modifiers_state(modifiers),
match state {
winit::event::ElementState::Pressed => {
keyboard::Event::KeyPressed {
key_code,
modifiers,
}
}
winit::event::ElementState::Released => {
keyboard::Event::KeyReleased {
key_code,
modifiers,
}
}
}
})), })),
WindowEvent::HoveredFile(path) => { WindowEvent::HoveredFile(path) => {
Some(Event::Window(window::Event::FileHovered(path.clone()))) Some(Event::Window(window::Event::FileHovered(path.clone())))