Skip to content

Commit

Permalink
Improve simulation of key presses in macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike committed May 22, 2024
1 parent 83b2697 commit 2472683
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/helpers/macos/applescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,24 @@ const INDENT = ' ';
*/
exports.renderScript = function (command) {
validateCommand(command);
// Key presses must be scripted as a nested series of "key down"/"key up"
// commands rather than a linear sequence of "key code" commands because
// VoiceOver does not recognize key combinations using the latter API (e.g.
// `key code {123, 124}`).
const renderedKeyCodeLines = command.keyCodes
.map(code => {
const renderedKeyCode = KeyCode[code].toString();
const renderedModifiers = renderModifiers(command);
return `
${INDENT}${INDENT}key code ${renderedKeyCode}${renderedModifiers}`;
})
.join('');
.reverse()
.map((code) => KeyCode[code].toString())
.reduce((accum, next) => `key down ${next}\n${accum}\nkey up ${next}`, '')
.split('\n')
.map((line) => `${INDENT}${INDENT}${line}`)
.join('\n');

const script = `with timeout of ${APPLESCRIPT_TIMEOUT} seconds
${INDENT}tell application "System Events"${renderedKeyCodeLines}
${INDENT}tell application "System Events"
${renderedKeyCodeLines}
${INDENT}end tell
end timeout`;

return applescript(script);

/**
Expand Down

0 comments on commit 2472683

Please sign in to comment.