diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index b854275fec..81e60f2b56 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,5 +1,6 @@ use iced::highlighter; -use iced::keyboard; +use iced::keyboard::key::Code; +use iced::keyboard::key::Physical; use iced::widget::{ button, center_x, column, container, operation, pick_list, row, space, text, text_editor, toggler, tooltip, @@ -212,8 +213,8 @@ impl Editor { self.theme, ) .key_binding(|key_press| { - match key_press.key.as_ref() { - keyboard::Key::Character("s") + match key_press.physical_key { + Physical::Code(Code::KeyS) if key_press.modifiers.command() => { Some(text_editor::Binding::Custom( diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index ef3a1429fc..7124d22829 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -36,6 +36,7 @@ use crate::core::clipboard::{self, Clipboard}; use crate::core::input_method; use crate::core::keyboard; use crate::core::keyboard::key; +use crate::core::keyboard::key::{Code, Physical}; use crate::core::layout::{self, Layout}; use crate::core::mouse; use crate::core::renderer; @@ -1123,6 +1124,8 @@ pub struct KeyPress { pub text: Option, /// The current [`Status`] of the [`TextEditor`]. pub status: Status, + /// The physical key pressed. + pub physical_key: Physical, } impl Binding { @@ -1133,35 +1136,35 @@ impl Binding { modifiers, text, status, + physical_key, } = event; if !matches!(status, Status::Focused { .. }) { return None; } - match key.as_ref() { - keyboard::Key::Named(key::Named::Enter) => Some(Self::Enter), - keyboard::Key::Named(key::Named::Backspace) => { + match physical_key { + Physical::Code(Code::Enter) => Some(Self::Enter), + Physical::Code(Code::Backspace) => { Some(Self::Backspace) } - keyboard::Key::Named(key::Named::Delete) + Physical::Code(Code::Delete) if text.is_none() || text.as_deref() == Some("\u{7f}") => { Some(Self::Delete) } - keyboard::Key::Named(key::Named::Escape) => Some(Self::Unfocus), - keyboard::Key::Character("c") if modifiers.command() => { + Physical::Code(Code::Escape) => Some(Self::Unfocus), + Physical::Code(Code::KeyC) if modifiers.command() => { Some(Self::Copy) } - keyboard::Key::Character("x") if modifiers.command() => { + Physical::Code(Code::KeyX) if modifiers.command() => { Some(Self::Cut) } - keyboard::Key::Character("v") - if modifiers.command() && !modifiers.alt() => + Physical::Code(Code::KeyV) if modifiers.command() && !modifiers.alt() => { Some(Self::Paste) } - keyboard::Key::Character("a") if modifiers.command() => { + Physical::Code(Code::KeyA) if modifiers.command() => { Some(Self::SelectAll) } _ => { @@ -1304,6 +1307,7 @@ impl Update { key, modifiers, text, + physical_key, .. }) => { let status = if state.focus.is_some() { @@ -1319,6 +1323,7 @@ impl Update { modifiers: *modifiers, text: text.clone(), status, + physical_key: physical_key.clone(), }; if let Some(key_binding) = key_binding { diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 0783f7c297..b75edef319 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -45,6 +45,7 @@ use crate::core::clipboard::{self, Clipboard}; use crate::core::input_method; use crate::core::keyboard; use crate::core::keyboard::key; +use crate::core::keyboard::key::{Code, Physical}; use crate::core::layout; use crate::core::mouse::{self, click}; use crate::core::renderer; @@ -873,15 +874,15 @@ where } } Event::Keyboard(keyboard::Event::KeyPressed { - key, text, .. + key, text, physical_key, .. }) => { let state = state::(tree); if let Some(focus) = &mut state.is_focused { let modifiers = state.keyboard_modifiers; - match key.as_ref() { - keyboard::Key::Character("c") + match physical_key { + Physical::Code(Code::KeyC) if state.keyboard_modifiers.command() && !self.is_secure => { @@ -897,7 +898,7 @@ where shell.capture_event(); return; } - keyboard::Key::Character("x") + Physical::Code(Code::KeyX) if state.keyboard_modifiers.command() && !self.is_secure => { @@ -926,7 +927,7 @@ where update_cache(state, &self.value); return; } - keyboard::Key::Character("v") + Physical::Code(Code::KeyV) if state.keyboard_modifiers.command() && !state.keyboard_modifiers.alt() => { @@ -965,7 +966,7 @@ where update_cache(state, &self.value); return; } - keyboard::Key::Character("a") + Physical::Code(Code::KeyA) if state.keyboard_modifiers.command() => { let cursor_before = state.cursor; @@ -1231,11 +1232,11 @@ where } } } - Event::Keyboard(keyboard::Event::KeyReleased { key, .. }) => { + Event::Keyboard(keyboard::Event::KeyReleased { physical_key, .. }) => { let state = state::(tree); if state.is_focused.is_some() - && let keyboard::Key::Character("v") = key.as_ref() + && let Physical::Code(Code::KeyV) = physical_key { state.is_pasting = None;