fix(macos): resolve paste/copy key codes for non-QWERTY layouts - #1822
Draft
io41 wants to merge 1 commit into
Draft
fix(macos): resolve paste/copy key codes for non-QWERTY layouts#1822io41 wants to merge 1 commit into
io41 wants to merge 1 commit into
Conversation
macos-fast-paste.swift hardcoded 0x09 for 'v' and 0x08 for 'c', which are only correct on QWERTY. On Dvorak, Colemak and similar layouts the posted Cmd+V / Cmd+C landed on the wrong key, so paste and selection capture silently did the wrong thing. Look the key code up in the current keyboard layout via UCKeyTranslate instead. Falls back to the QWERTY key code for the same mode if the lookup fails, so a failed lookup never turns a copy into a paste.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
resources/macos-fast-paste.swifthardcodes the virtual key codes it posts:0x09forvand0x08forc. Those values are positions on a QWERTY layout, not characters. On Dvorak, Colemak, and similar layouts they land on different keys, so the synthesized⌘Vand⌘Care not paste and copy.Both paths are affected:
--copyselection capture returnsCOPY_OKbecause the frontmost app resolved fine, but the clipboard was never populated — so the caller believes it captured a selection that it did not.On a Dvorak layout,
vis at key code 47 andcis at key code 34.Fix
Look the key code up in the active keyboard layout with
UCKeyTranslateinstead of hardcoding it.keyCodeForCharacter(_:)walks key codes 0–127 through the currentTISCopyCurrentKeyboardLayoutInputSource()layout and returns the one that produces the requested character.If the lookup fails, it falls back to the QWERTY key code for the same mode —
0x08in copy mode,0x09in paste mode. Keeping the fallback mode-aligned matters: falling back to a single constant would let a failed lookup in copy mode post⌘V, overwriting the user's selection instead of copying it.No behaviour change on QWERTY: the lookup returns the same values that were previously hardcoded.
Notes
import Carbonfor the Text Input Services andUCKeyTranslateAPIs.--copystdout contract (COPY_OK <pid> <name>), exit codes, and the ordering of theAXIsProcessTrusted()check and frontmost-app resolution are all unchanged.Testing
swiftccompiles clean.vto key code 47 andcto key code 34, confirming the resolution path runs rather than silently falling back.