forked from github/hotkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotkey.js
More file actions
31 lines (30 loc) · 1020 Bytes
/
hotkey.js
File metadata and controls
31 lines (30 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* @flow strict */
// # Returns a hotkey character string for keydown and keyup events.
//
// A full list of key names can be found here:
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
//
// ## Code Example
//
// ```
// document.addEventListener('keydown', function(event) {
// if (hotkey(event) === 'h') ...
// })
// ```
// ## Hotkey examples
//
// "s" // Lowercase character for single letters
// "S" // Uppercase character for shift plus a letter
// "1" // Number character
// "?" // Shift plus "/" symbol
//
// "Enter" // Enter key
// "ArrowUp" // Up arrow
//
// "Control+s" // Control modifier plus letter
// "Control+Alt+Delete" // Multiple modifiers
//
// Returns key character String or null.
export default function hotkey(event: KeyboardEvent) {
return `${event.ctrlKey ? 'Control+' : ''}${event.altKey ? 'Alt+' : ''}${event.metaKey ? 'Meta+' : ''}${event.key}`
}