macos: Handle ctrl characters in IME input (#4854)

Fixes:
https://github.com/ghostty-org/ghostty/issues/4634#issuecomment-2573469532

This commit fixes two issues:

1. `libghostty` must not override ctrl+key inputs if we are in a preedit
state. This allows thigs like `ctrl+h` to work properly in an IME.

2. On macOS, when an IME commits text, we strip the control modifier
from the key event we send to libghostty. This is a bit of a hack but
this avoids triggering special ctrl+key handling.
This commit is contained in:
Mitchell Hashimoto
2025-01-08 22:07:15 -08:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

View File

@ -828,8 +828,28 @@ extension Ghostty {
var handled: Bool = false
if let list = keyTextAccumulator, list.count > 0 {
handled = true
// 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: event, text: text)
_ = keyAction(action, event: keyTextEvent, text: text)
}
}
}

View File

@ -199,6 +199,11 @@ pub const App = struct {
// This logic only applies to macOS.
if (comptime builtin.os.tag != .macos) break :event_text event.text;
// If we're in a preedit state then we allow it through. This
// allows ctrl sequences that affect IME to work. For example,
// Ctrl+H deletes a character with Japanese input.
if (event.composing) break :event_text event.text;
// If the modifiers are ONLY "control" then we never process
// the event text because we want to do our own translation so
// we can handle ctrl+c, ctrl+z, etc.