项目作者: kripod

项目描述 :
Frictionless hotkey handling for browsers
高级语言: TypeScript
项目地址: git://github.com/kripod/keez.git
创建时间: 2020-12-12T16:06:48Z
项目社区:https://github.com/kripod/keez

开源协议:MIT License

下载


keez

Frictionless hotkey handling for browsers

npm
npm bundle size

Usage

  1. import { captureKeys } from "keez";
  2. const saveCommand = captureKeys("CmdOrCtrl", "S");
  3. const italicCommand = captureKeys("CmdOrCtrl", "I");
  4. document.addEventListener("keydown", (event) => {
  5. if (saveCommand(event)) {
  6. /* Do something, e.g. call `fetch` */
  7. } else if (italicCommand(event)) {
  8. /* Do something else, e.g. format selected text */
  9. }
  10. });

Features

  • CmdOrCtrl modifier for interoperability between operating systems
  • Supports synthetic events (e.g. for React elements)
  • Calls event.preventDefault() when a match is found, suppressing handlers of the underlying browser (or even the system)
  • TypeScript-based code completion for common modifier keys
  • Low overhead, compared to similar libraries

Browser support

Every browser with the Set built-in is supported out of the box.

Implementation details

There are quite a few attributes to handle keystrokes with:

Layout-aware Modifier-independent Supports all events Named non-printables
key
code
keyCode / which (legacy)
charCode (legacy)

Despite being a legacy attribute, keyCode is used in comparisons by default. It’s independent from modifiers, unlike the modern key alternative.

However, when it comes to named key attribute values (e.g. Escape or Backspace), the key property is used under the hood.