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

View File

@ -532,24 +532,19 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
self.trigger_resize(layout, cursor_position, messages);
}
Event::Keyboard(keyboard::Event::Input {
Event::Keyboard(keyboard::Event::KeyPressed {
modifiers,
key_code,
state,
}) => {
if let Some(on_key_press) = &self.on_key_press {
// TODO: Discard when event is captured
if state == ButtonState::Pressed {
if let Some(_) = self.state.active_pane() {
if modifiers.matches(self.modifier_keys) {
if let Some(message) =
on_key_press(KeyPressEvent {
key_code,
modifiers,
})
{
messages.push(message);
}
if let Some(_) = self.state.active_pane() {
if modifiers.matches(self.modifier_keys) {
if let Some(message) = on_key_press(KeyPressEvent {
key_code,
modifiers,
}) {
messages.push(message);
}
}
}
@ -557,6 +552,11 @@ where
*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());
messages.push(message);
}
Event::Keyboard(keyboard::Event::Input {
Event::Keyboard(keyboard::Event::KeyPressed {
key_code,
state: ButtonState::Pressed,
modifiers,
}) if self.state.is_focused => match key_code {
keyboard::KeyCode::Enter => {
@ -473,10 +472,8 @@ where
}
_ => {}
},
Event::Keyboard(keyboard::Event::Input {
key_code,
state: ButtonState::Released,
..
Event::Keyboard(keyboard::Event::KeyReleased {
key_code, ..
}) => match key_code {
keyboard::KeyCode::V => {
self.state.is_pasting = None;

View File

@ -70,10 +70,24 @@ pub fn window_event(
..
},
..
} => Some(Event::Keyboard(keyboard::Event::Input {
key_code: key_code(*virtual_keycode),
state: button_state(*state),
modifiers: modifiers_state(modifiers),
} => Some(Event::Keyboard({
let key_code = key_code(*virtual_keycode);
let 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) => {
Some(Event::Window(window::Event::FileHovered(path.clone())))