Skip to content

Commit

Permalink
Improve simulation of key presses in macOS (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored May 22, 2024
1 parent 83b2697 commit 1d40d52
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
37 changes: 15 additions & 22 deletions lib/helpers/macos/applescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,25 @@ const INDENT = ' ';
*/
exports.renderScript = function (command) {
validateCommand(command);
const renderedKeyCodeLines = command.keyCodes
.map(code => {
const renderedKeyCode = KeyCode[code].toString();
const renderedModifiers = renderModifiers(command);
return `
${INDENT}${INDENT}key code ${renderedKeyCode}${renderedModifiers}`;
})
.join('');
// 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.modifiers, ...command.keyCodes]
.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);

/**
* @param {Applescript.AppleKeyCodeCommand} command
* @returns {string}
*/
function renderModifiers(command) {
if (command.modifiers.length > 1) {
return ` using {${command.modifiers.map(mod => `${Modifier[mod]} down`).join(', ')}}`;
} else if (command.modifiers.length === 1) {
return ` using ${Modifier[command.modifiers[0]]} down`;
}
return '';
}
return applescript(script);
};

/**
Expand Down
20 changes: 17 additions & 3 deletions test/helpers/macos/applescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ suite('helpers/macos/applescript', () => {
applescript(
'with timeout of 5 seconds\n' +
' tell application "System Events"\n' +
' key code 49\n' +
' key down 49\n' +
' \n' +
' key up 49\n' +
' end tell\n' +
'end timeout',
),
Expand All @@ -67,7 +69,11 @@ suite('helpers/macos/applescript', () => {
applescript(
'with timeout of 5 seconds\n' +
' tell application "System Events"\n' +
' key code 0 using option down\n' +
' key down 58\n' +
' key down 0\n' +
' \n' +
' key up 0\n' +
' key up 58\n' +
' end tell\n' +
'end timeout',
),
Expand All @@ -80,7 +86,15 @@ suite('helpers/macos/applescript', () => {
applescript(
'with timeout of 5 seconds\n' +
' tell application "System Events"\n' +
' key code 11 using {command down, option down, shift down}\n' +
' key down 55\n' +
' key down 58\n' +
' key down 57\n' +
' key down 11\n' +
' \n' +
' key up 11\n' +
' key up 57\n' +
' key up 58\n' +
' key up 55\n' +
' end tell\n' +
'end timeout',
),
Expand Down

0 comments on commit 1d40d52

Please sign in to comment.