Use an easy-to-read key combo string to capture keyboard events. For example: `⇧ ⌥ ⌘ + K` → `e.shiftKey && e.altKey && e.metaKey && e.code === 'KeyK'`
npm install keycombo-parseWhen alt key is involved in to key combination, the value of e.key becomes strange. For example, hitting ⌥ + A, you'll get å as e.key value. The e.code value is more accurate in this case: KeyA, but a bit lengthy.
While registering a keyboard shorcut event, devolopers want something like this:
``js
import kc from "keycombo-parse"
document.addEventListener("keydown", e => {
if (kc("⇧ ⌥ ⌘ + C", e)) {
// shortcut activated
}
})
`
The key combination string: ⇧ ⌥ ⌘ + C, is quite succinct, but might be hard to type for those not using a text expander such as espanso. In this package we also support key combo string in plain text: shift alt meta + C.
After installation with pnpm add keycombo-parse, you could import it to browser client-side JavaScript code:
`js
// using the default export
import isKeyComboMatch from "keycombo-parse"
document.addEventListener("keydown", e => {
if (isKeyComboMatch("⇧ ⌥ ⌘ + H", e)) {
showHelp()
}
})
const showHelp() => console.log("Displaying help dialog...")
`
or get the standardized keycombo string:
`js
import { standardKeyCombo } from "keycombo-parse"
standardKeyCombo(" cmd shift option + k") // log output: ⇧ ⌥ ⌘ + K
`
if you want have a shortcut that requires hitting content key twice, define it this way:
`js
import isKeyComboMatch from "keycombo-parse"
document.addEventListener("keydown", e => {
if (isKeyComboMatch("⌘ + C", e)) {
copyName()
} else if (isKeyComboMatch("⌘ + CC", e)) {
copySVG()
}
})
const copyName = () => console.log("Copying icon name...")
const copySVG = () => console.log("Copying icon SVG...")
`
A few things to note:
- We use space as separator, so spaces are necessary
- We recognize alt, option, or ⌥, all supported!ShiftLeft
- We avoid differentiating modifier keys on the right side, so and ShiftRight are treated as the sameAlt
- Case-insensitive, so and alt are both acceptable⌘ + C
- It's recommended that key combination always end with a non-modifier key, for example: , ⌘ + 1, or ⌘ + shift
- A single non-modifer key are supported only when user not typing into a text field
- A single modifer key: , alt, meta, ctrl are also supported
- Function keys are not supported
![
and its alternativesWhen naming the key combo string, you can use the complete key code name, for example: Shift Meta + KeyC, or the more concise couterpart: ⇧ ⌘ + C.
- Digit0: 0, )1
- Digit1: , !2
- Digit2: , @3
- Digit3: , #4
- Digit4: , $5
- Digit5: , %6
- Digit6: , ^7
- Digit7: , &8
- Digit8: , *9
- Digit9: , (
- KeyQ: Q, qW
- KeyW: , wE
- KeyE: , eR
- KeyR: , rT
- KeyT: , tY
- KeyY: , yU
- KeyU: , uI
- KeyI: , iO
- KeyO: , oP
- KeyP: , pA
- KeyA: , aS
- KeyS: , sD
- KeyD: , dF
- KeyF: , fG
- KeyG: , gH
- KeyH: , hJ
- KeyJ: , jK
- KeyK: , kL
- KeyL: , lZ
- KeyZ: , zX
- KeyX: , xC
- KeyC: , cV
- KeyV: , vB
- KeyB: , bN
- KeyN: , nM
- KeyM: , m
- Space: space, ␣enter
- Enter: , ⏎,↵,↩, returntab
- Tab: , ⇥
- BracketLeft: [, {}
- BracketRight: , ]-
- Minus: , _+
- Equal: , =;
- Semicolon: , :,
- Comma: , <.
- Period: , >'
- Quote: , "~
- Backquote: , (can't display normally in markdown code format)
- Backslash: \, |
- Slash: /, ?
- Escape: esc, escape
- Backspace: ⌫, backspace, delete, del
- CapsLock: caps, cap, capslock, ⇪
- ArrowUp: arrowup, up, ↑
- ArrowDown: arrowdown, down, ↓
- ArrowLeft: arrowleft, left, ←
- ArrowRight: arrowright, right, →
- AltLeft: alt, option, ⌥
- ShiftLeft: shift, ⇧
- ControlLeft: control, ctrl, ⌃
- MetaLeft: command, cmd, ⌘, meta