1.0.0 • Published 3 years ago

rx-hotkey v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

rx-hotkey

A simple hotkey library.

Coverage Status npm version minzipped size

Install

npm install rx-hotkey

Usage

To bind or unbind hotkeys:

import {hotkey, createHotkey} from 'rx-hotkey'

// bind global hotkey
const unbind = hotkey('?', showHelp)
unbind()

// combo, with modifiers
hotkey('ctrl+r', (event) => {})
// sequences, space separated keys
hotkey('c i', (...events) => {})
hotkey('meta+k meta+v', (...events) => {})

// bind scoped hotkey
const scopedHotkey = createHotkey(sidebarElement)
const unbind = scopedHotkey('g h', goHome)
unbind()

To get or listen bindings:

import {hotkey, getKeyBindings, listenKeyBindings} from 'rx-hotkey'

hotkey('ctrl+r', (event) => {}, {meta: 1})
const scopedHotkey = createHotkey(sidebarElement, {scope: 'sidebar'})
const unbind = scopedHotkey('g h', goHome, {meta: 2})

getKeyBindings()
/* =>
Map {
  "global" => Map {
    "ctrl+r" => Object {meta: 1},
  },
  "sidebar" => Map {
    "g h" => Object {meta: 2},
  },
}
*/

// listen changes of key bindings
const unlisten = listenKeyBindings(handler)
unlisten()

Examples