All Projects → devlato → react-shortcut

devlato / react-shortcut

Licence: MIT license
Convenient React component that detects if the given key combination is pressed, and triggers a callback

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-shortcut

Keymage
Yet Another JS Keybinding library
Stars: ✭ 325 (+1931.25%)
Mutual labels:  hotkeys, shortcut, hotkey, shortcuts
hotkey
⌨️ cross-platform hotkey package
Stars: ✭ 82 (+412.5%)
Mutual labels:  hotkeys, shortcut, hotkey, shortcuts
keybind
ClojureScript key bindings (shortcut) library
Stars: ✭ 85 (+431.25%)
Mutual labels:  hotkeys, shortcut, hotkey, shortcuts
Hotkeys
➷ A robust Javascript library for capturing keyboard input. It has no dependencies.
Stars: ✭ 5,165 (+32181.25%)
Mutual labels:  hotkeys, shortcut, hotkey, shortcuts
react-hotkey-tooltip
A global Hotkey provider with built in tooltip for React
Stars: ✭ 34 (+112.5%)
Mutual labels:  hotkeys, shortcut, hotkey
Xcactionbar
"Alfred for Xcode" plugin
Stars: ✭ 1,217 (+7506.25%)
Mutual labels:  shortcut, hotkey, shortcuts
Chordly
Chordly is a javascript library that may be used to detect and act upon key sequences entered by a user.
Stars: ✭ 14 (-12.5%)
Mutual labels:  hotkeys, konami
webplayer-hotkeys
A Chrome extension that assigns hotkeys to play/pause, and switch musics (next and previous). Works with Spotify, Deezer, SoundCloud and Youtube
Stars: ✭ 16 (+0%)
Mutual labels:  hotkeys, hotkey
swhkd
Sxhkd clone for Wayland (works on TTY and X11 too) | mirrored at https://git.sr.ht/~shinyzenith/swhkd
Stars: ✭ 215 (+1243.75%)
Mutual labels:  hotkeys, hotkey
Capslock Plus
An efficiency tool that provides various functions by enhancing the Caps Lock key into a modifier key.
Stars: ✭ 650 (+3962.5%)
Mutual labels:  shortcut, hotkey
react-keyboard-shortcuts
A declarative library for handling hotkeys based on explicit priority in React applications
Stars: ✭ 23 (+43.75%)
Mutual labels:  hotkeys, shortcuts
React Hotkeys
React component to listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts.
Stars: ✭ 279 (+1643.75%)
Mutual labels:  hotkeys, shortcut
Keyboard
Hook and simulate global keyboard events on Windows and Linux.
Stars: ✭ 2,687 (+16693.75%)
Mutual labels:  callback, hotkey
g910-gkey-macro-support
GKey support for Logitech G910 Keyboard on Linux
Stars: ✭ 85 (+431.25%)
Mutual labels:  hotkeys, shortcut
Autohotkey
⚙️ My Autohotkey productivity suite that includes shortcuts, hotstrings, hotkeys, apps/utilities, AutoCorrect
Stars: ✭ 113 (+606.25%)
Mutual labels:  hotkeys, shortcut
Win Vind
Simple Vim Key Binder for Windows. You can operate Windows with keybindings like Vim.
Stars: ✭ 151 (+843.75%)
Mutual labels:  hotkeys, hotkey
Electron Localshortcut
Add keyboard shortcuts locally to a BrowserWindow instance, without using a Menu
Stars: ✭ 366 (+2187.5%)
Mutual labels:  shortcut, shortcuts
Python Shortcuts
Create Siri Shortcuts with Python
Stars: ✭ 525 (+3181.25%)
Mutual labels:  shortcut, shortcuts
static-hands
⌨️🤯 Stop moving hands, and start code fast ⚡⚡
Stars: ✭ 36 (+125%)
Mutual labels:  shortcut, shortcuts
Hotkeys
🤖 A declarative library for handling hotkeys in Angular applications
Stars: ✭ 158 (+887.5%)
Mutual labels:  hotkeys, shortcuts

react-shortcut

Convenient React component that detects if the given key combination is pressed, and triggers a callback

View on npm Master Build Status Release CI Status Maintainability Test Coverage Demo Bundle size Downloads MIT License Issues

Installation

With npm:

$ npm install --save react-shortcut

Or with Yarn:

$ yarn add react-shortcut

Using the component

Is very simple and straightforward! There are just a couple of props to pass in.

Code example

import ReactShortcut from 'react-shortcut';

// ...
// Somewhere in your component tree
<ReactShortcut
  keys={/* String or array of strings containing the keys to be pressed, in any supported format */}
  onKeysPressed={/* Callback when target key combination is pressed */}
/>;

Props

All the props are required.

Name Description Type
keys A string containing comma-separated key combinations or/and key sequences, or an array of such strings A string or an array of strings
onKeysPressed A callback to be triggered when the user presses any of the specified key combinations A function with no arguments

Key combinations and Key sequences

The component supports both key combinations and key sequences.

Key combinations

A key combination is a string of key names separated by a plus sign, that describes what keys the user has to press at the same time, to execute the callback specified using onKeysPressed prop.

Examples: Command+Shift+Left, Ctrl+P.

To react on the keys combination(s) press, use the following format:

import ReactShortcut from 'react-shortcut';

// Pass in the shortcut keys
<ReactShortcut
    keys="command+k"
    onKeysPressed={doSomethingOnShortcutPress}
/>

// ... or an array of shortcuts
<ReactShortcut
    keys={['command+k', 'command+m']}
    onKeysPressed={doSomethingOnShortcutPress}
/>

// ... or a string of comma-separated shortcuts
<ReactShortcut
    keys="command+k,command+m"
    onKeysPressed={doSomethingOnShortcutPress}
/>

Key sequences

A key sequence is a string of key names separated by a space character, that lists out the keys the user has to press one by one, to trigger the callback specified using onKeysPressed prop.

Examples: Up Up Down Down Left Right Left Right B A Enter, k o n a m i.

To react on the keys sequence(s) press, use the following format:

import ReactShortcut from 'react-shortcut';

// Pass in the shortcut keys
<ReactShortcut
    keys="k o n a m i"
    onKeysPressed={doSomethingOnShortcutPress}
/>

// ... or an array of shortcuts
<ReactShortcut
    keys={['k o n a m i', 'm a r i o b r o s enter']}
    onKeysPressed={doSomethingOnShortcutPress}
/>

// ... or a string of comma-separated shortcuts
<ReactShortcut
    keys="k o n a m i,m a r i o b r o s enter"
    onKeysPressed={doSomethingOnShortcutPress}
/>

Mixed use

Mixing both modes is possible –just follow the same key combination/key sequence convention:

import ReactShortcut from 'react-shortcut';

// Array of shortcuts
<ReactShortcut
    keys={['k o n a m i', 'shift+command+m']}
    onKeysPressed={doSomethingOnShortcutPress}
/>

// ... or a string of comma-separated shortcuts
<ReactShortcut
    keys="k o n a m i,shift+command+m"
    onKeysPressed={doSomethingOnShortcutPress}
/>

FAQ

Does it support TypeScript?

It does. Moreover, it's implemented in TypeScript.

Do I have to use component only in the root level component?

Nope. The component adds a global keyboard event listener and doesn't prevent events from bubbling or capturing.

What if my app needs to support multiple shortcuts?

Just use the component as many times as you need, just make sure the shortcuts aren't repeated.

Do I have to specify the shortcuts in lower case only?

No, the case doesn't matter.

Any open-source examples of using this library?

There's an official™️ one called react-easter, for adding easter eggs triggered by the keypress.

License

The library is shipped "as is" under MIT License.

Contributing contributions welcome

Feel free to contribute, but don't forget to write tests, mate/matess.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].