From ab948f6f8fe36edb16c55b6858b70d1accbecba3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 14 Nov 2023 08:14:24 -0800 Subject: [PATCH] macos: reuse original event if translation mods changes nothing Fixes #766 (again) This is a repeat fix of 766 after fixing a number of other regressions from the original fix. See the comment for reasons. --- macos/Sources/Ghostty/SurfaceView.swift | 37 +++++++++++++++---------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index 7702cffd7..b1a8a69ab 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -749,20 +749,29 @@ extension Ghostty { translationMods.remove(flag) } } - - // Build a new NSEvent we use only for translation - let translationEvent = NSEvent.keyEvent( - with: event.type, - location: event.locationInWindow, - modifierFlags: translationMods, - timestamp: event.timestamp, - windowNumber: event.windowNumber, - context: nil, - characters: event.characters(byApplyingModifiers: translationMods) ?? "", - charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? "", - isARepeat: event.isARepeat, - keyCode: event.keyCode - ) ?? event + + // If the translation modifiers are not equal to our original modifiers + // then we need to construct a new NSEvent. If they are equal we reuse the + // old one. IMPORTANT: we MUST reuse the old event if they're equal because + // this keeps things like Korean input working. There must be some object + // equality happening in AppKit somewhere because this is required. + let translationEvent: NSEvent + if (translationMods == event.modifierFlags) { + translationEvent = event + } else { + translationEvent = NSEvent.keyEvent( + with: event.type, + location: event.locationInWindow, + modifierFlags: translationMods, + timestamp: event.timestamp, + windowNumber: event.windowNumber, + context: nil, + characters: event.characters(byApplyingModifiers: translationMods) ?? "", + charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? "", + isARepeat: event.isARepeat, + keyCode: event.keyCode + ) ?? event + } // By setting this to non-nil, we note that we'rein a keyDown event. From here, // we call interpretKeyEvents so that we can handle complex input such as Korean