Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/editor/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 15 additions & 10 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1123,6 +1124,8 @@ pub struct KeyPress {
pub text: Option<SmolStr>,
/// The current [`Status`] of the [`TextEditor`].
pub status: Status,
/// The physical key pressed.
pub physical_key: Physical,
}

impl<Message> Binding<Message> {
Expand All @@ -1133,35 +1136,35 @@ impl<Message> Binding<Message> {
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)
}
_ => {
Expand Down Expand Up @@ -1304,6 +1307,7 @@ impl<Message> Update<Message> {
key,
modifiers,
text,
physical_key,
..
}) => {
let status = if state.focus.is_some() {
Expand All @@ -1319,6 +1323,7 @@ impl<Message> Update<Message> {
modifiers: *modifiers,
text: text.clone(),
status,
physical_key: physical_key.clone(),
};

if let Some(key_binding) = key_binding {
Expand Down
17 changes: 9 additions & 8 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -873,15 +874,15 @@ where
}
}
Event::Keyboard(keyboard::Event::KeyPressed {
key, text, ..
key, text, physical_key, ..
}) => {
let state = state::<Renderer>(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 =>
{
Expand All @@ -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 =>
{
Expand Down Expand Up @@ -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() =>
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1231,11 +1232,11 @@ where
}
}
}
Event::Keyboard(keyboard::Event::KeyReleased { key, .. }) => {
Event::Keyboard(keyboard::Event::KeyReleased { physical_key, .. }) => {
let state = state::<Renderer>(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;

Expand Down