Tiny, dependency-free library to manage keyboard shortcuts in your application.
- Install the library from npm registry:
npm install --save shortcutter
- Import the necessary parts into your project:
import {PHASES, useShortcutter} from 'shortcutter';
- Use
shortcutter
to start using shortcuts:
const shortcutter = useShortcutter();
- 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,
);
- Open your test application in browser, and press A + S
The string-type PHASES
contains all possible variants of the shortcut's status.
Variant | Value |
---|---|
PHASES.DOWN |
down |
PHASES.PRESS |
press |
PHASES.UP |
up |
PHASES.DOWN_PRESS |
down|press |
PHASES.DOWN_UP |
down|up |
PHASES.PRESS_UP |
press|up |
PHASES.DOWN_PRESS_UP |
down|press|up |
Returns interface to control contexts and shortcuts.
const {
listen,
unlisten,
hasContext,
getActiveContext
setActiveContext
} = useShortcutter();
Adds a listener for defined keys into the shortcutter's context.
Arguments | Type | Description |
---|---|---|
context | string |
--- |
keys | string[] |
--- |
callback | (event: Event, phases: PHASES) => void |
--- |
phases | PHASES |
--- |
Returns shortcutter.unlisten
.
Removes a listener for defined keys from the shortcutter's context.
Arguments | Type | Description |
---|---|---|
context | string |
--- |
keys | string[] |
--- |
phases | PHASES |
--- |
Checks if a passed context is already added to Shortcutter
.
Arguments | Type | Description |
---|---|---|
name | string |
--- |
Returns active context's name.
Sets passed context as the active one.
Arguments | Type | Description |
---|---|---|
name | string |
--- |
This library follows Semantic Versioning.
Shortcutter
is available under the MIT license. See the LICENSE file for more details.
Crafted with ❤️ to 📋 and 🔑