diff --git a/crates/egui/src/data/input/keyboard_shortcut.rs b/crates/egui/src/data/input/keyboard_shortcut.rs index 3998c5c4..704db65b 100644 --- a/crates/egui/src/data/input/keyboard_shortcut.rs +++ b/crates/egui/src/data/input/keyboard_shortcut.rs @@ -34,6 +34,36 @@ impl KeyboardShortcut { } s } + + /// Would consuming this shortcut interfere with editing text + /// (e.g. moving the text cursor, or typing a space)? + /// + /// This is true for any shortcut without modifiers (e.g. `Space`, `A`, `Delete`), + /// as well as for shortcuts using keys that are commonly combined with modifiers + /// when editing text (e.g. `Alt+ArrowLeft` to move one word, or `Shift+Home` to select to the start of a line). + /// + /// Such shortcuts should only be consumed when no text field has keyboard focus. + /// See also [`crate::Memory::focused`] and [`crate::Context::egui_wants_keyboard_input`]. + /// + /// ``` + /// # use egui::{Key, KeyboardShortcut, Modifiers}; + /// assert!(KeyboardShortcut::new(Modifiers::NONE, Key::Space).conflicts_with_text_editing()); + /// assert!(KeyboardShortcut::new(Modifiers::ALT, Key::ArrowLeft).conflicts_with_text_editing()); + /// assert!(!KeyboardShortcut::new(Modifiers::COMMAND, Key::S).conflicts_with_text_editing()); + /// ``` + pub fn conflicts_with_text_editing(&self) -> bool { + self.modifiers.is_none() + || matches!( + self.logical_key, + Key::Space + | Key::ArrowLeft + | Key::ArrowRight + | Key::ArrowUp + | Key::ArrowDown + | Key::Home + | Key::End + ) + } } #[test]