Skip to content

Commit 00d4249

Browse files
committed
Format keyboard shortcuts with glyphs and phrases according to platform conventions.
Fixes jupyterlab#151
1 parent 76f615f commit 00d4249

File tree

1 file changed

+61
-31
lines changed

1 file changed

+61
-31
lines changed

packages/commands/src/index.ts

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,33 +1172,23 @@ export namespace CommandRegistry {
11721172
* Format a keystroke for display on the local system.
11731173
*/
11741174
export function formatKeystroke(keystroke: string): string {
1175-
let mods = '';
1175+
let mods = [];
1176+
let separator = Platform.IS_MAC ? " " : "+";
11761177
let parts = parseKeystroke(keystroke);
1177-
if (Platform.IS_MAC) {
1178-
if (parts.ctrl) {
1179-
mods += '\u2303 ';
1180-
}
1181-
if (parts.alt) {
1182-
mods += '\u2325 ';
1183-
}
1184-
if (parts.shift) {
1185-
mods += '\u21E7 ';
1186-
}
1187-
if (parts.cmd) {
1188-
mods += '\u2318 ';
1189-
}
1190-
} else {
1191-
if (parts.ctrl) {
1192-
mods += 'Ctrl+';
1193-
}
1194-
if (parts.alt) {
1195-
mods += 'Alt+';
1196-
}
1197-
if (parts.shift) {
1198-
mods += 'Shift+';
1199-
}
1178+
if (parts.ctrl) {
1179+
mods.push('Ctrl');
12001180
}
1201-
return mods + parts.key;
1181+
if (parts.alt) {
1182+
mods.push('Alt');
1183+
}
1184+
if (parts.shift) {
1185+
mods.push('Shift');
1186+
}
1187+
if (Platform.IS_MAC && parts.cmd) {
1188+
mods.push('Cmd');
1189+
}
1190+
mods.push(parts.key);
1191+
return mods.map(Private.formatKey).join(separator);
12021192
}
12031193

12041194
/**
@@ -1228,20 +1218,21 @@ export namespace CommandRegistry {
12281218
if (!key || layout.isModifierKey(key)) {
12291219
return '';
12301220
}
1231-
let mods = '';
1221+
let mods = [];
12321222
if (event.ctrlKey) {
1233-
mods += 'Ctrl ';
1223+
mods.push('Ctrl')
12341224
}
12351225
if (event.altKey) {
1236-
mods += 'Alt ';
1226+
mods.push('Alt')
12371227
}
12381228
if (event.shiftKey) {
1239-
mods += 'Shift ';
1229+
mods.push('Shift')
12401230
}
12411231
if (event.metaKey && Platform.IS_MAC) {
1242-
mods += 'Cmd ';
1232+
mods.push('Cmd')
12431233
}
1244-
return mods + key;
1234+
mods.push(key);
1235+
return mods.join(" ");
12451236
}
12461237
}
12471238

@@ -1438,6 +1429,45 @@ namespace Private {
14381429
event.target!.dispatchEvent(cloneKeyboardEvent(event));
14391430
}
14401431

1432+
export function formatKey(key: string): string {
1433+
if (Platform.IS_MAC) {
1434+
return MAC_DISPLAY.hasOwnProperty(key) ? MAC_DISPLAY[key] : key;
1435+
} else {
1436+
return WIN_DISPLAY.hasOwnProperty(key) ? WIN_DISPLAY[key] : key;
1437+
}
1438+
}
1439+
1440+
const MAC_DISPLAY: { [key: string]: string } = {
1441+
Backspace: '⌫',
1442+
Tab: '⇥',
1443+
Enter: '↩',
1444+
Shift: '⇧',
1445+
Ctrl: '⌃',
1446+
Alt: '⌥',
1447+
Escape: '⎋',
1448+
PageUp: '⇞',
1449+
PageDown: '⇟',
1450+
End: '↘',
1451+
Home: '↖',
1452+
ArrowLeft: '←',
1453+
ArrowUp: '↑',
1454+
ArrowRight: '→',
1455+
ArrowDown: '↓',
1456+
Delete: '⌦',
1457+
Cmd: '⌘'
1458+
};
1459+
1460+
const WIN_DISPLAY: { [key: string]: string } = {
1461+
Escape: 'Esc',
1462+
PageUp: 'Page Up',
1463+
PageDown: 'Page Down',
1464+
ArrowLeft: 'Left',
1465+
ArrowUp: 'Right',
1466+
ArrowRight: 'Up',
1467+
ArrowDown: 'Down',
1468+
Delete: 'Del'
1469+
};
1470+
14411471
/**
14421472
* A singleton empty string function.
14431473
*/

0 commit comments

Comments
 (0)