From 9978ea3b9c0312fe4a27bf818d8fc988441bcaba Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 13 Feb 2025 14:20:13 -0800 Subject: [PATCH] Revert "macos: don't remove ctrl modifier for text input" This reverts commit 3104b217581a9e580155216a260b1a79135b701a. --- .../Sources/Ghostty/SurfaceView_AppKit.swift | 24 +++++++++++++++++-- src/apprt/embedded.zig | 18 +++++++------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 0adc11fa4..f5cb93580 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -849,8 +849,28 @@ extension Ghostty { var handled: Bool = false if let list = keyTextAccumulator, list.count > 0 { handled = true - for text in list { - _ = keyAction(action, event: event, text: text) + + // This is a hack. libghostty on macOS treats ctrl input as not having + // text because some keyboard layouts generate bogus characters for + // ctrl+key. libghostty can't tell this is from an IM keyboard giving + // us direct values. So, we just remove control. + var modifierFlags = event.modifierFlags + modifierFlags.remove(.control) + if let keyTextEvent = NSEvent.keyEvent( + with: .keyDown, + location: event.locationInWindow, + modifierFlags: modifierFlags, + timestamp: event.timestamp, + windowNumber: event.windowNumber, + context: nil, + characters: event.characters ?? "", + charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? "", + isARepeat: event.isARepeat, + keyCode: event.keyCode + ) { + for text in list { + _ = keyAction(action, event: keyTextEvent, text: text) + } } } diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index ffcf7fdbe..c4e69f917 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -182,9 +182,14 @@ pub const App = struct { if (strip) translate_mods.alt = false; } - // We strip super on macOS because its not used for translation - // it results in a bad translation. + // On macOS we strip ctrl because UCKeyTranslate + // converts to the masked values (i.e. ctrl+c becomes 3) + // and we don't want that behavior. + // + // We also strip super because its not used for translation + // on macos and it results in a bad translation. if (comptime builtin.target.isDarwin()) { + translate_mods.ctrl = false; translate_mods.super = false; } @@ -231,14 +236,7 @@ pub const App = struct { .surface => |surface| &surface.keymap_state, }, @intCast(keycode), - if (comptime builtin.target.isDarwin()) mods: { - // On macOS we strip ctrl because UCKeyTranslate - // converts to the masked values (i.e. ctrl+c becomes 3) - // and we don't want that behavior. - var v = translate_mods; - v.ctrl = false; - break :mods v; - } else translate_mods, + translate_mods, ); // TODO(mitchellh): I think we can get rid of the above keymap