0.2.0 • Published 2 years ago

shortcutter v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

SHORTCUTTER

Tiny, dependency-free library to manage keyboard shortcuts in your application.

Getting started

1) Install the library from npm registry:

npm install --save shortcutter

2) Import the necessary parts into your project:

import {PHASES, useShortcutter} from 'shortcutter';

3) Use shortcutter to start using shortcuts:

const shortcutter = useShortcutter();

4) Add your first key binding

shortcutter.listen(
  // context, where you would like to add a shortcut
  'default', 
  // keys, which invoke callback
  ['a', 's'],
  // callback which `shortcutter` should invoke
  (event: Event, phase: PHASES) => alert(`My first key binding on ${phase}`),
  // phases when `shortcutter` should invoke callback 
  PHASES.DOWN_PRESS_UP,
);

5) Open your test application in browser, and press A + S

API

Types

PHASES

The string-type PHASES contains all possible variants of the shortcut's status.

VariantValue
PHASES.DOWNdown
PHASES.PRESSpress
PHASES.UPup
PHASES.DOWN_PRESSdown\|press
PHASES.DOWN_UPdown\|up
PHASES.PRESS_UPpress\|up
PHASES.DOWN_PRESS_UPdown\|press\|up

Interfaces

useShortcutter

Returns interface to control contexts and shortcuts.

const {
  listen,
  unlisten,
  hasContext,
  getActiveContext
  setActiveContext
} = useShortcutter();

shortcutter.listen

Adds a listener for defined keys into the shortcutter's context.

ArgumentsTypeDescription
contextstring---
keysstring[]---
callback(event: Event, phases: PHASES) => void---
phasesPHASES---

Returns shortcutter.unlisten.

shortcutter.unlisten

Removes a listener for defined keys from the shortcutter's context.

ArgumentsTypeDescription
contextstring---
keysstring[]---
phasesPHASES---

shortcutter.hasContext

Checks if a passed context is already added to Shortcutter.

ArgumentsTypeDescription
namestring---

shortcutter.getActiveContext

Returns active context's name.

shortcutter.setActiveContext

Sets passed context as the active one.

ArgumentsTypeDescription
namestring---

Versioning

This library follows Semantic Versioning.

License

Shortcutter is available under the MIT license. See the LICENSE file for more details.

Crafted with :heart: to :clipboard: and :key: