Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ permissions:
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ for (const el of document.querySelectorAll('[data-shortcut]')) {
7. `"Plus"` and `"Space"` are special key names to represent the `+` and ` ` keys respectively, because these symbols cannot be represented in the normal hotkey string syntax.
8. You can use the comma key `,` as a hotkey, e.g. `a,,` would activate if the user typed `a` or `,`. `Control+,,x` would activate for `Control+,` or `x`.
9. `"Shift"` should be included if it would be held and the key is uppercase: ie, `Shift+A` not `A`
1. MacOS outputs lowercase key names when `Meta+Shift` is held (ie, `Meta+Shift+a`). In an attempt to normalize this, `hotkey` will automatically map these key names to uppercase, so the uppercase keys should still be used (ie, `"Meta+Shift+A"` or `"Mod+Shift+A"`). **However**, this normalization only works on US keyboard layouts.
1. On macOS, modifier+Shift combinations may report lowercase letters. `hotkey` normalizes ASCII letters to uppercase, so use `"Meta+Shift+A"` or `"Mod+Shift+A"`.
2. Shift normalization leaves other characters unchanged, so use the reported key (for example, `"Meta+Shift+1"` if `event.key` is `"1"`). macOS `Alt` combinations still use a US-layout Option mapping, which may differ on other layouts.

### Example

Expand Down
4 changes: 2 additions & 2 deletions src/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export function eventToHotkeyString(
const altNormalizedKey =
hotkeyString.includes('Alt') && matchApplePlatform.test(platform) ? macosSymbolLayerKeys[key] ?? key : key

// MacOS outputs lowercase characters when `Command+Shift` is held, so we map them back to uppercase if we can
// macOS may report lowercase letters when a modifier and `Shift` are held.
const shiftNormalizedKey =
hotkeyString.includes('Shift') && matchApplePlatform.test(platform)
shiftKey && (ctrlKey || altKey || metaKey) && matchApplePlatform.test(platform)
? macosUppercaseLayerKeys[altNormalizedKey] ?? altNormalizedKey
: altNormalizedKey

Expand Down
28 changes: 1 addition & 27 deletions src/macos-uppercase-layer.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
/**
* Map of 'uppercase' symbols to the keys that would be pressed (while holding `Shift`) to type them on MacOS on an
* English layout. Most of these are standardized across most language layouts, so this won't work 100% in every
* language but it should work most of the time.
*
*/
/** Map ASCII lowercase letters to uppercase for macOS modifier+Shift events. */
export const macosUppercaseLayerKeys: Record<string, string> = {
['`']: '~',
['1']: '!',
['2']: '@',
['3']: '#',
['4']: '$',
['5']: '%',
['6']: '^',
['7']: '&',
['8']: '*',
['9']: '(',
['0']: ')',
['-']: '_',
['=']: '+',
['[']: '{',
[']']: '}',
['\\']: '|',
[';']: ':',
["'"]: '"',
[',']: '<',
['.']: '>',
['/']: '?',
['q']: 'Q',
['w']: 'W',
['e']: 'E',
Expand Down
83 changes: 52 additions & 31 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,37 +263,49 @@ describe('hotkey', function () {

describe('eventToHotkeyString', function () {
const tests = [
['Control+Shift+J', {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'J'}],
['Control+Shift+j', {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'j'}],
['Control+j', {ctrlKey: true, code: 'KeyJ', key: 'j'}],
['Meta+Shift+p', {key: 'p', metaKey: true, shiftKey: true, code: 'KeyP'}],
['Meta+Shift+8', {key: '8', metaKey: true, shiftKey: true, code: 'Digit8'}],
['Control+Shift+7', {key: '7', ctrlKey: true, shiftKey: true, code: 'Digit7'}],
['Shift+J', {shiftKey: true, code: 'KeyJ', key: 'J'}],
['/', {key: '/', code: ''}],
['1', {key: '1', code: 'Digit1'}],
['Control+Shift+`', {ctrlKey: true, shiftKey: true, key: '`'}],
['c', {key: 'c', code: 'KeyC'}],
['Shift+S', {key: 'S', shiftKey: true, code: 'KeyS'}],
['Shift+!', {key: '!', shiftKey: true, code: 'Digit1'}],
['Control+Shift', {ctrlKey: true, shiftKey: true, key: 'Shift'}],
['Control+Shift', {ctrlKey: true, shiftKey: true, key: 'Control'}],
['Alt+s', {altKey: true, key: 's'}],
['Alt+s', {altKey: true, key: 'ß'}, 'mac'],
['Alt+Shift+S', {altKey: true, shiftKey: true, key: 'S'}],
['Alt+Shift+S', {altKey: true, shiftKey: true, key: 'Í'}, 'mac'],
['Alt+ArrowLeft', {altKey: true, key: 'ArrowLeft'}],
['Alt+ArrowLeft', {altKey: true, key: 'ArrowLeft'}, 'mac'],
['Alt+Shift+ArrowLeft', {altKey: true, shiftKey: true, key: 'ArrowLeft'}],
['Alt+Shift+ArrowLeft', {altKey: true, shiftKey: true, key: 'ArrowLeft'}, 'mac'],
['Control+Space', {ctrlKey: true, key: ' '}],
['Shift+Plus', {shiftKey: true, key: '+'}],
['Meta+Shift+X', {metaKey: true, shiftKey: true, key: 'x'}, 'mac'],
['Control+Shift+X', {ctrlKey: true, shiftKey: true, key: 'X'}],
['Meta+Shift+!', {metaKey: true, shiftKey: true, key: '1'}, 'mac'],
['Control+Shift+!', {ctrlKey: true, shiftKey: true, key: '!'}]
// Other platforms
{expected: '/', event: {key: '/', code: ''}},
{expected: '1', event: {key: '1', code: 'Digit1'}},
{expected: 'c', event: {key: 'c', code: 'KeyC'}},
{expected: 'Shift+J', event: {shiftKey: true, code: 'KeyJ', key: 'J'}},
{expected: 'Shift+S', event: {key: 'S', shiftKey: true, code: 'KeyS'}},
{expected: 'Shift+!', event: {key: '!', shiftKey: true, code: 'Digit1'}},
{expected: 'Shift+Plus', event: {shiftKey: true, key: '+'}},
{expected: 'Control+j', event: {ctrlKey: true, code: 'KeyJ', key: 'j'}},
{expected: 'Control+Space', event: {ctrlKey: true, key: ' '}},
{expected: 'Control+Shift+J', event: {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'J'}},
{expected: 'Control+Shift+j', event: {ctrlKey: true, shiftKey: true, code: 'KeyJ', key: 'j'}},
{expected: 'Control+Shift+7', event: {key: '7', ctrlKey: true, shiftKey: true, code: 'Digit7'}},
{expected: 'Control+Shift+`', event: {ctrlKey: true, shiftKey: true, key: '`'}},
{expected: 'Control+Shift', event: {ctrlKey: true, shiftKey: true, key: 'Shift'}},
{expected: 'Control+Shift', event: {ctrlKey: true, shiftKey: true, key: 'Control'}},
{expected: 'Control+Shift+X', event: {ctrlKey: true, shiftKey: true, key: 'X'}},
{expected: 'Control+Shift+!', event: {ctrlKey: true, shiftKey: true, key: '!'}},
{expected: 'Alt+s', event: {altKey: true, key: 's'}},
{expected: 'Alt+ArrowLeft', event: {altKey: true, key: 'ArrowLeft'}},
{expected: 'Alt+Shift+S', event: {altKey: true, shiftKey: true, key: 'S'}},
{expected: 'Alt+Shift+ArrowLeft', event: {altKey: true, shiftKey: true, key: 'ArrowLeft'}},
{expected: 'Meta+Shift+p', event: {key: 'p', metaKey: true, shiftKey: true, code: 'KeyP'}},
{expected: 'Meta+Shift+8', event: {key: '8', metaKey: true, shiftKey: true, code: 'Digit8'}},

// macOS
{expected: 'Shift+A', event: {shiftKey: true, key: 'A', code: 'KeyA'}, platform: 'mac'},
{expected: 'Shift+/', event: {shiftKey: true, key: '/', code: 'Digit7'}, platform: 'mac'}, // DE keyboard layout
{expected: 'Shift+?', event: {shiftKey: true, key: '?', code: 'Minus'}, platform: 'mac'}, // DE keyboard layout
{expected: 'Shift+(', event: {shiftKey: true, key: '(', code: 'Digit8'}, platform: 'mac'}, // DE keyboard layout
{expected: 'Shift+Plus', event: {shiftKey: true, key: '+', code: 'BracketRight'}, platform: 'mac'}, // DE keyboard layout
{expected: 'Control+Shift+/', event: {ctrlKey: true, shiftKey: true, key: '/'}, platform: 'mac'},
{expected: 'Alt+s', event: {altKey: true, key: 'ß'}, platform: 'mac'},
{expected: 'Alt+ArrowLeft', event: {altKey: true, key: 'ArrowLeft'}, platform: 'mac'},
{expected: 'Alt+Shift+S', event: {altKey: true, shiftKey: true, key: 'Í'}, platform: 'mac'},
{expected: 'Alt+Shift+ArrowLeft', event: {altKey: true, shiftKey: true, key: 'ArrowLeft'}, platform: 'mac'},
{expected: 'Alt+Shift+/', event: {altKey: true, shiftKey: true, key: '/'}, platform: 'mac'},
{expected: 'Meta+Shift+X', event: {metaKey: true, shiftKey: true, key: 'x'}, platform: 'mac'},
{expected: 'Meta+Shift+1', event: {metaKey: true, shiftKey: true, key: '1'}, platform: 'mac'},
{expected: 'Meta+Shift+!', event: {metaKey: true, shiftKey: true, key: '!'}, platform: 'mac'},
{expected: 'Meta+Shift+/', event: {metaKey: true, shiftKey: true, key: '/'}, platform: 'mac'}
]
for (const [expected, keyEvent, platform = 'win / linux'] of tests) {
for (const {expected, event: keyEvent, platform = 'win / linux'} of tests) {
it(`${JSON.stringify(keyEvent)} => ${expected}`, () => {
return new Promise(resolve => {
document.body.addEventListener('keydown', function handler(event) {
Expand Down Expand Up @@ -384,14 +396,23 @@ describe('hotkey', function () {
})

it('works with macos meta+shift plane', async () => {
setHTML(`<a href="#" id="metashiftplane" data-hotkey="Meta+Shift+p"></a>`)
const key = /Mac|iPod|iPhone|iPad/i.test(navigator.platform) ? 'P' : 'p'
setHTML(`<a href="#" id="metashiftplane" data-hotkey="Meta+Shift+${key}"></a>`)

document.dispatchEvent(new KeyboardEvent('keydown', {metaKey: true, shiftKey: true, key: 'p'}))

await wait(10)

expect(elementsActivated).toEqual(['metashiftplane'])
})

it('matches the reported non-letter key with meta+shift', function () {
setHTML('<a href="#" id="metashift1" data-hotkey="Meta+Shift+1"></a>')

document.dispatchEvent(new KeyboardEvent('keydown', {metaKey: true, shiftKey: true, key: '1'}))

expect(elementsActivated).toEqual(['metashift1'])
})
})

describe('elements', function () {
Expand Down