-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(computer): preserve modifier keys through VNC #3097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
quanru
wants to merge
2
commits into
main
Choose a base branch
from
fix/computer-vnc-physical-keyboard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import { getDebug } from '@midscene/shared/logger'; | ||
| import { US_SHIFTED_CHARACTER_KEYS } from './keyboard-layout'; | ||
|
|
||
| const debugKeyboard = getDebug('computer:keyboard'); | ||
|
|
||
| const APPLE_SCRIPT_KEY_CODES: Readonly<Partial<Record<string, number>>> = { | ||
| return: 36, | ||
| enter: 36, | ||
| tab: 48, | ||
| space: 49, | ||
| backspace: 51, | ||
| delete: 51, | ||
| escape: 53, | ||
| forwarddelete: 117, | ||
| left: 123, | ||
| right: 124, | ||
| down: 125, | ||
| up: 126, | ||
| home: 115, | ||
| end: 119, | ||
| pageup: 116, | ||
| pagedown: 121, | ||
| f1: 122, | ||
| f2: 120, | ||
| f3: 99, | ||
| f4: 118, | ||
| f5: 96, | ||
| f6: 97, | ||
| f7: 98, | ||
| f8: 100, | ||
| f9: 101, | ||
| f10: 109, | ||
| f11: 103, | ||
| f12: 111, | ||
| }; | ||
|
|
||
| const APPLE_SCRIPT_MODIFIER_KEYS: Readonly<Partial<Record<string, string>>> = { | ||
| command: 'command', | ||
| cmd: 'command', | ||
| control: 'control', | ||
| ctrl: 'control', | ||
| shift: 'shift', | ||
| alt: 'option', | ||
| option: 'option', | ||
| meta: 'command', | ||
| }; | ||
|
|
||
| /** | ||
| * Modifier delivery mode for the macOS AppleScript keyboard backend. | ||
| * | ||
| * `logical` is the default for native macOS applications. `physical` emits | ||
| * explicit modifier transitions for VNC clients and assumes an en-US mapping | ||
| * for shifted punctuation; it should not be enabled for native applications. | ||
| */ | ||
| export type KeyboardEventMode = 'logical' | 'physical'; | ||
|
|
||
| function buildKeyCommand(key: string): string { | ||
| const keyCode = APPLE_SCRIPT_KEY_CODES[key.toLowerCase()]; | ||
| if (keyCode !== undefined) { | ||
| return `key code ${keyCode}`; | ||
| } | ||
|
|
||
| const escapedKey = key.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | ||
| return `keystroke "${escapedKey}"`; | ||
| } | ||
|
|
||
| function resolveModifierKeys(modifiers: string[]): string[] { | ||
| return modifiers | ||
| .map((modifier) => APPLE_SCRIPT_MODIFIER_KEYS[modifier.toLowerCase()]) | ||
| .filter((modifier): modifier is string => modifier !== undefined); | ||
| } | ||
|
|
||
| function buildLogicalKeyPress(key: string, modifiers: string[]): string { | ||
| const modifierKeys = resolveModifierKeys(modifiers); | ||
| const modifierClause = modifierKeys.length | ||
| ? ` using {${modifierKeys | ||
| .map((modifier) => `${modifier} down`) | ||
| .join(', ')}}` | ||
| : ''; | ||
| return `tell application "System Events" to ${buildKeyCommand(key)}${modifierClause}`; | ||
| } | ||
|
|
||
| function resolvePhysicalKey( | ||
| key: string, | ||
| modifiers: string[], | ||
| ): { key: string; modifiers: string[] } { | ||
| const resolvedModifiers = [...modifiers]; | ||
| const shiftedBaseKey = US_SHIFTED_CHARACTER_KEYS.get(key); | ||
|
|
||
| if (/^[A-Z]$/.test(key)) { | ||
| resolvedModifiers.push('shift'); | ||
| return { key: key.toLowerCase(), modifiers: resolvedModifiers }; | ||
| } | ||
| if (shiftedBaseKey !== undefined) { | ||
| resolvedModifiers.push('shift'); | ||
| return { key: shiftedBaseKey, modifiers: resolvedModifiers }; | ||
| } | ||
| return { key, modifiers: resolvedModifiers }; | ||
| } | ||
|
|
||
| function buildPhysicalKeyPress(key: string, modifiers: string[]): string { | ||
| const resolved = resolvePhysicalKey(key, modifiers); | ||
| const modifierKeys = [...new Set(resolveModifierKeys(resolved.modifiers))]; | ||
| const keyCommand = buildKeyCommand(resolved.key); | ||
|
|
||
| if (modifierKeys.length === 0) { | ||
| return `tell application "System Events" to ${keyCommand}`; | ||
| } | ||
|
|
||
| const releaseCommands = [...modifierKeys] | ||
| .reverse() | ||
| .map((modifier) => `key up ${modifier}`); | ||
| return [ | ||
| 'tell application "System Events"', | ||
| 'try', | ||
| ...modifierKeys.map((modifier) => `key down ${modifier}`), | ||
| keyCommand, | ||
| 'on error errorMessage number errorNumber', | ||
| ...releaseCommands, | ||
| 'error errorMessage number errorNumber', | ||
| 'end try', | ||
| ...releaseCommands, | ||
| 'end tell', | ||
| ].join('\n'); | ||
| } | ||
|
|
||
| /** @internal exported for focused unit tests */ | ||
| export function buildAppleScriptKeyPress( | ||
| key: string, | ||
| modifiers: string[] = [], | ||
| eventMode: KeyboardEventMode = 'logical', | ||
| ): string { | ||
| return eventMode === 'physical' | ||
| ? buildPhysicalKeyPress(key, modifiers) | ||
| : buildLogicalKeyPress(key, modifiers); | ||
| } | ||
|
|
||
| /** Send one key press through macOS System Events without invoking a shell. */ | ||
| export function sendKeyViaAppleScript( | ||
| key: string, | ||
| modifiers: string[] = [], | ||
| eventMode: KeyboardEventMode = 'logical', | ||
| ): void { | ||
| const script = buildAppleScriptKeyPress(key, modifiers, eventMode); | ||
| debugKeyboard('sendKeyViaAppleScript', { | ||
| key, | ||
| modifiers, | ||
| eventMode, | ||
| script, | ||
| }); | ||
| execFileSync('osascript', ['-e', script]); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When physical mode targets a Windows VNC session and
KeyboardPressreceives a shortcut such asWindows+R,normalizeKeyName()converts the modifier towin, but this map has nowinentry;resolveModifierKeys()then silently filters it out and sends onlyr. Mapwinto the physical Command/Meta key, or reject it explicitly, so the shortcut is not silently altered.AGENTS.md reference: AGENTS.md:L8-L8
Useful? React with 👍 / 👎.