MCPcopy
hub / github.com/github/hotkey / eventToHotkeyString

Function eventToHotkeyString

src/hotkey.ts:38–68  ·  view source on GitHub ↗
(
  event: KeyboardEvent,
  platform: string = navigator.platform
)

Source from the content-addressed store, hash-verified

36 * })
37 */
38export function eventToHotkeyString(
39 event: KeyboardEvent,
40 platform: string = navigator.platform
41): NormalizedHotkeyString {
42 const {ctrlKey, altKey, metaKey, shiftKey, key} = event
43 const hotkeyString: string[] = []
44 const modifiers: boolean[] = [ctrlKey, altKey, metaKey, shiftKey]
45
46 for (const [i, mod] of modifiers.entries()) {
47 if (mod) hotkeyString.push(modifierKeyNames[i])
48 }
49
50 if (!modifierKeyNames.includes(key)) {
51 // MacOS outputs symbols when `Alt` is held, so we map them back to the key symbol if we can
52 const altNormalizedKey =
53 hotkeyString.includes('Alt') && matchApplePlatform.test(platform) ? macosSymbolLayerKeys[key] ?? key : key
54
55 // MacOS outputs lowercase characters when `Command+Shift` is held, so we map them back to uppercase if we can
56 const shiftNormalizedKey =
57 hotkeyString.includes('Shift') && matchApplePlatform.test(platform)
58 ? macosUppercaseLayerKeys[altNormalizedKey] ?? altNormalizedKey
59 : altNormalizedKey
60
61 // Some symbols can't be used because of hotkey string format, so we replace them with 'synthetic' named keys
62 const syntheticKey = syntheticKeyNames[shiftNormalizedKey] ?? shiftNormalizedKey
63
64 hotkeyString.push(syntheticKey)
65 }
66
67 return hotkeyString.join('+') as NormalizedHotkeyString
68}
69
70const modifierKeyNames: string[] = ['Control', 'Alt', 'Meta', 'Shift']
71

Callers 3

test.jsFile · 0.90
keyDownHandlerFunction · 0.85
registerKeypressMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected