macos/applescript: derive key event text and codepoint#13180
Conversation
Previously, when executing the `send key` AppleScript command, the generated key events only contained the key code and modifiers, but lacked both the input text representation and the unshifted codepoint. This caused the terminal to ignore or fail to process printable characters sent via AppleScript. Now, use `UCKeyTranslate` to look up the current keyboard layout and derive both the simulated input text and the unshifted codepoint associated with the key code. These are then passed to the resulting key event, enabling proper typing and input handling for keys sent via AppleScript.
|
I don't quite get what your use case is. Can you make an example of where current implementation doesn't work? And be careful, this pr description already violates our ai policy. |
@bo2themax Of course, I'll trim the description and add a detailed explanation where current implementation breaks. |
|
@bo2themax I've updated the description. Let me know if you have any questions. Cheers! |
87f98f6 to
de0bcac
Compare
|
|
||
| /// extracted translation logic to map a Ghostty key to its generated text and unshifted codepoint, | ||
| /// primarily so it can be unit-tested without instantiating an `NSScriptCommand`. | ||
| struct ScriptKeyEventTranslator { |
There was a problem hiding this comment.
I don't think we need UCKeyTranslate here, ppl who use it should know which key they are trying to type.
There was a problem hiding this comment.
KeymapDarwin.zig already using it
There was a problem hiding this comment.
Let me clarify one thing.
Are you saying that the below should be able to derive .text in KeymapDarwin.zig (via UCKeyTranslate)?
let keyEvent = Ghostty.Input.KeyEvent(
key: key,
action: action,
mods: mods
)And my use case/repro should work without the fix?
|
Please see #13205, I don't like using |
Awesome! I'll test it in my setup. |
Executing the
sendkey AppleScript command is broken for printable characters. The generatedKeyEventlacks both text representation andunshiftedCodepoint. Consequently, Ghostty ignores or failed to process letters and spaces sent via AppleScript.Minimal repro (doesn't work as expected with
stableandtip):Warning
This sends
dateto all opened terminals/surfacesThis, however, does work (from documentation):
It has a different effect when one wants to broadcast on character per character (e.g. synchronise
two or more Kakoune or NeoVim, etc)
This PR fixes that by using
UCKeyTranslateto retrieve the current keyboard layout, translate the simulated keycodes, and correctly populate these missing fields before sending the key event.I have tested this locally using the US International keyboard layout as well as US proper, and it successfully translates the keycodes. I'd appreciate if someone with German or French layout test it.
I have also added some smoke tests to verify the AppleScript key event mapping. These are isolated in a separate commit; if you find them too noisy, I am happy to get rid of them.
An AppleScript
sendkey command that cannot type standard letters is not particularly useful. Ensuring key events carry their respective text and codepoints is necessary for basic automation to function.Note
I am also happy to create a tracking issue if Ghosttty's workflow requires it.
Changes
• ScriptKeyEventCommand.swift : Translated key codes to character text and unshifted codepoints using UCKeyTranslate .
• KeyEventCommandTests.swift: Added basic unit tests
AI Disclosure
Important
This PR was prepared using Antigravity CLI. The AI helped to discover
UCKeyTranslateCarbon API.