From 5c1aff114305fe978e8e8987cb46cf4a87e7e2c3 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 2 Mar 2022 14:08:25 -0800 Subject: [PATCH] Don't insert input into editors when control or command keys are held This is a follow-up to #475 --- crates/gpui/src/platform/mac/event.rs | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/crates/gpui/src/platform/mac/event.rs b/crates/gpui/src/platform/mac/event.rs index 9ba231d532..7c870e4720 100644 --- a/crates/gpui/src/platform/mac/event.rs +++ b/crates/gpui/src/platform/mac/event.rs @@ -21,14 +21,19 @@ impl Event { match event_type { NSEventType::NSKeyDown => { - let mut input = None; let modifiers = native_event.modifierFlags(); + let ctrl = modifiers.contains(NSEventModifierFlags::NSControlKeyMask); + let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask); + let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask); + let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask); + let unmodified_chars = CStr::from_ptr( native_event.charactersIgnoringModifiers().UTF8String() as *mut c_char, ) .to_str() .unwrap(); + let mut input = None; let unmodified_chars = if let Some(first_char) = unmodified_chars.chars().next() { use cocoa::appkit::*; const BACKSPACE_KEY: u16 = 0x7f; @@ -71,14 +76,16 @@ impl Event { NSF12FunctionKey => "f12", _ => { - input = Some( - CStr::from_ptr( - native_event.characters().UTF8String() as *mut c_char - ) - .to_str() - .unwrap() - .into(), - ); + if !cmd && !ctrl { + input = Some( + CStr::from_ptr( + native_event.characters().UTF8String() as *mut c_char + ) + .to_str() + .unwrap() + .into(), + ); + } unmodified_chars } } @@ -88,10 +95,10 @@ impl Event { Some(Self::KeyDown { keystroke: Keystroke { - ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), - alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), - shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), - cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask), + ctrl, + alt, + shift, + cmd, key: unmodified_chars.into(), }, input,