Skip to content

Commit bc70e3d

Browse files
authored
Merge pull request #112 from github/plus-space
Add support for `Space` and `Plus`
2 parents 8b64334 + 506b997 commit bc70e3d

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ for (const el of document.querySelectorAll('[data-shortcut]')) {
109109
1. `"Mod+"` can appear in any order in a hotkey string. For example: `"Mod+Alt+Shift+KEY"`
110110
2. Neither the `Control` or `Meta` modifiers should appear in a hotkey string with `Mod`.
111111
3. Due to the inconsistent lowercasing of `event.key` on Mac and iOS when `Meta` is pressed along with `Shift`, it is recommended to avoid hotkey strings containing both `Mod` and `Shift`.
112-
7. 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`.
112+
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.
113+
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`.
113114

114115
### Example
115116

src/hotkey.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const normalizedHotkeyBrand = Symbol('normalizedHotkey')
2222
*/
2323
export type NormalizedHotkeyString = NormalizedSequenceString & {[normalizedHotkeyBrand]: true}
2424

25+
const syntheticKeyNames: Record<string, string> = {
26+
' ': 'Space',
27+
'+': 'Plus'
28+
}
29+
2530
/**
2631
* Returns a hotkey character string for keydown and keyup events.
2732
* @example
@@ -43,7 +48,8 @@ export function eventToHotkeyString(
4348

4449
if (!modifierKeyNames.includes(key)) {
4550
const nonOptionPlaneKey = matchApplePlatform.test(platform) ? macosSymbolLayerKeys[key] ?? key : key
46-
hotkeyString.push(nonOptionPlaneKey)
51+
const syntheticKey = syntheticKeyNames[nonOptionPlaneKey] ?? nonOptionPlaneKey
52+
hotkeyString.push(syntheticKey)
4753
}
4854

4955
return hotkeyString.join('+') as NormalizedHotkeyString

test/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ describe('hotkey', function () {
284284
['Alt+ArrowLeft', {altKey: true, key: 'ArrowLeft'}],
285285
['Alt+ArrowLeft', {altKey: true, key: 'ArrowLeft'}, 'mac'],
286286
['Alt+Shift+ArrowLeft', {altKey: true, shiftKey: true, key: 'ArrowLeft'}],
287-
['Alt+Shift+ArrowLeft', {altKey: true, shiftKey: true, key: 'ArrowLeft'}, 'mac']
287+
['Alt+Shift+ArrowLeft', {altKey: true, shiftKey: true, key: 'ArrowLeft'}, 'mac'],
288+
['Control+Space', {ctrlKey: true, key: ' '}],
289+
['Shift+Plus', {shiftKey: true, key: '+'}]
288290
]
289291
for (const [expected, keyEvent, platform = 'win / linux'] of tests) {
290292
it(`${JSON.stringify(keyEvent)} => ${expected}`, function (done) {

0 commit comments

Comments
 (0)