Skip to content

macos: translate physical menu shortcuts - #13888

Merged
bo2themax merged 4 commits into
ghostty-org:mainfrom
jparise:macos-translate-keyboard-shortcuts
Aug 24, 2026
Merged

bo2themax merged 4 commits into
ghostty-org:mainfrom
jparise:macos-translate-keyboard-shortcuts

Conversation

@jparise

@jparise jparise commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Translate printable physical keybindings through the current macOS keyboard layout before assigning menu key equivalents. Previously these bindings could not be represented because SwiftUI shortcuts are character-based, so actions such as super+backquote had no native menu shortcut.

Keep native keycodes as dispatch identity so translated display characters do not change physical semantics or precedence over Unicode bindings. Refresh shortcuts when the input source changes, and prevent AppKit from transforming equivalents that are already localized.

AI Usage: The approach was suggested by GPT 5.6 Sol, but I wrote most of the code and understand it all.

Translate printable physical keybindings through the current macOS
keyboard layout before assigning menu key equivalents. Previously these
bindings could not be represented because SwiftUI shortcuts are
character-based, so actions such as super+backquote had no native menu
shortcut.

Keep native keycodes as dispatch identity so translated display characters
do not change physical semantics or precedence over Unicode bindings.
Refresh shortcuts when the input source changes, and prevent AppKit from
transforming equivalents that are already localized.
Separate menu shortcut presentation from lookup identity. Store either a
normalized key equivalent or a physical keycode in a private hashable enum,
allowing Swift to synthesize equality and hashing instead of maintaining
parallel optional-key logic.

Assign display characters directly from KeyboardShortcut and remove unused
NSMenuItem and SwiftUI conversion helpers.
@jparise
jparise requested a review from a team as a code owner August 18, 2026 15:58
@jparise jparise added os/macos input Keyboard or mouse input labels Aug 18, 2026
Text Input Sources APIs are not thread-safe, but shortcut translation could
be called outside a declared main-actor context.

Mark keyboard layout and shortcut conversion as main-actor isolated, update
their tests, and dispatch key-sequence UI notifications to the main queue
before translating their shortcuts.

@bo2themax bo2themax left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this also relates to #13180 and then #13205. But as I said in the comments there in #13180, core already use UCKeyTranslate. I wonder if there's a way to avoid this kinda duplication.

The changes looks good to me, but I haven't run it locally and want a second opinion on this.

Translate synthetic physical-key events with characters(byApplyingModifiers:).
This preserves current-layout and Command-table behavior while avoiding a
duplicate direct UCKeyTranslate implementation in Swift.
@jparise

jparise commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor Author

I believe this also relates to #13180 and then #13205. But as I said in the comments there in #13180, core already use UCKeyTranslate. I wonder if there's a way to avoid this kinda duplication.

That's good feedback. I remember seeing #13180 go by but should have revisited it more closely.

I revised this to use AppKit’s characters(byApplyingModifiers:), avoiding the UCKeyTranslate duplication. Going further to reuse the code from core seems like it would net out to more overall complexity.

@bo2themax

Copy link
Copy Markdown
Member

Going further to reuse the code from core seems like it would net out to more overall complexity.

Yeah, true🫪

@jparise
jparise requested a review from bo2themax August 20, 2026 14:13
Comment thread macos/Tests/Ghostty/NormalizedMenuShortcutKeyTests.swift
@bo2themax
bo2themax merged commit 6a508fd into ghostty-org:main Aug 24, 2026
101 checks passed
@github-actions github-actions Bot added this to the 1.4.0 milestone Aug 24, 2026
@jparise
jparise deleted the macos-translate-keyboard-shortcuts branch August 24, 2026 13:07
mitchellh added a commit that referenced this pull request Aug 25, 2026
…iers:) (#14009)

Follow up for #13888, and prepare for #13205.

The comments are copied from the history commit.

## AI Disclosure

The tests are updated by Claude, I cherrypicked them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

input Keyboard or mouse input os/macos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants