From 27523ef3ea94f2da536046c5404ed7e0adf080b8 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 1 Nov 2023 09:43:39 -0600 Subject: [PATCH] Prevent shortcuts from being typed into document --- src/text_box.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/text_box.rs b/src/text_box.rs index bed23d8..34485ab 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -3,7 +3,7 @@ use cosmic::{ iced::{ event::{Event, Status}, - keyboard::{Event as KeyEvent, KeyCode}, + keyboard::{Event as KeyEvent, KeyCode, Modifiers}, mouse::{self, Button, Event as MouseEvent, ScrollDelta}, Color, Element, Length, Padding, Rectangle, Size, }, @@ -407,14 +407,20 @@ where } _ => (), }, + Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => { + state.modifiers = modifiers; + } Event::Keyboard(KeyEvent::CharacterReceived(character)) => { - match character { - '\n' | '\r' => {} - _ => { - editor.action(Action::Insert(character)); + // Only parse keys when Super, Ctrl, and Alt are not pressed + if !state.modifiers.logo() && !state.modifiers.control() && !state.modifiers.alt() { + match character { + '\n' | '\r' => {} + _ => { + editor.action(Action::Insert(character)); + } } + status = Status::Captured; } - status = Status::Captured; } Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => { if let Some(p) = cursor_position.position_in(layout.bounds()) { @@ -472,6 +478,7 @@ where } pub struct State { + modifiers: Modifiers, is_dragging: bool, scale_factor: Cell, handle: Mutex, @@ -481,6 +488,7 @@ impl State { /// Creates a new [`State`]. pub fn new() -> State { State { + modifiers: Modifiers::empty(), is_dragging: false, scale_factor: Cell::new(1.0), //TODO: make option!