From edb861634186936c677d48c79c95937929ad0d02 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Apr 2025 14:25:34 -0700 Subject: [PATCH] macos: translationMods should be used for consumed mods calculation Fixes #7131 Regression from #7121 Our consumed mods should not include "alt" if `macos-option-as-alt` is set. To do this, we need to calculate our consumed mods based on the actual translation event mods (if available, only available during keyDown). --- macos/Sources/Ghostty/NSEvent+Extension.swift | 12 ++++++++++-- macos/Sources/Ghostty/SurfaceView_AppKit.swift | 13 ++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/macos/Sources/Ghostty/NSEvent+Extension.swift b/macos/Sources/Ghostty/NSEvent+Extension.swift index 5c13003b3..041a5a199 100644 --- a/macos/Sources/Ghostty/NSEvent+Extension.swift +++ b/macos/Sources/Ghostty/NSEvent+Extension.swift @@ -6,7 +6,13 @@ extension NSEvent { /// /// This will not set the "text" or "composing" fields since these can't safely be set /// with the information or lifetimes given. - func ghosttyKeyEvent(_ action: ghostty_input_action_e) -> ghostty_input_key_s { + /// + /// The translationMods should be set to the modifiers used for actual character + /// translation if available. + func ghosttyKeyEvent( + _ action: ghostty_input_action_e, + translationMods: NSEvent.ModifierFlags? = nil, + ) -> ghostty_input_key_s { var key_ev: ghostty_input_key_s = .init() key_ev.action = action key_ev.keycode = UInt32(keyCode) @@ -22,7 +28,9 @@ extension NSEvent { // so far: control and command never contribute to the translation of text, // assume everything else did. key_ev.mods = Ghostty.ghosttyMods(modifierFlags) - key_ev.consumed_mods = Ghostty.ghosttyMods(modifierFlags.subtracting([.control, .command])) + key_ev.consumed_mods = Ghostty.ghosttyMods( + (translationMods ?? modifierFlags) + .subtracting([.control, .command])) // Our unshifted codepoint is the codepoint with no modifiers. We // ignore multi-codepoint values. diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 52314f534..e182e210f 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -961,13 +961,19 @@ extension Ghostty { // These never have "composing" set to true because these are the // result of a composition. for text in list { - _ = keyAction(action, event: translationEvent, text: text) + _ = keyAction( + action, + event: event, + translationEvent: translationEvent, + text: text + ) } } else { // We have no accumulated text so this is a normal key event. _ = keyAction( action, - event: translationEvent, + event: event, + translationEvent: translationEvent, text: translationEvent.ghosttyCharacters, composing: markedText.length > 0 ) @@ -1165,12 +1171,13 @@ extension Ghostty { private func keyAction( _ action: ghostty_input_action_e, event: NSEvent, + translationEvent: NSEvent? = nil, text: String? = nil, composing: Bool = false ) -> Bool { guard let surface = self.surface else { return false } - var key_ev = event.ghosttyKeyEvent(action) + var key_ev = event.ghosttyKeyEvent(action, translationMods: translationEvent?.modifierFlags) key_ev.composing = composing if let text { return text.withCString { ptr in