Bind Keyboard Shortcuts With Pure JavaScript – keycuts

Category: Javascript | July 16, 2018
Authormedihack
Last UpdateJuly 16, 2018
LicenseMIT
Views326 views
Bind Keyboard Shortcuts With Pure JavaScript – keycuts

keycuts is an ES6 JavaScript library which allows you to bind custom keyboard shortcut events and trigger custom specified functions when the key combinations are pressed

How to use it:

Download & Install the keycuts package with package managers.

# Yarn
$ yarn add keycuts
# NPM
$ npm install keycuts --save

Import the keycuts as an ES6 module.

import { Keys } from 'keycuts'

Or include the compiled version of the keycuts library on the page.

<script src="keycuts.min.js"></script>

Create a new keyboard shortcut object.

const myKeys = new Keys();

Define your own keyboard shortcuts & callback functions as these:

keys.bind(['Control', 'c'], () => {
  // do something
})
// or 
keys.bind(['Control + c'], () => {
  // do something
})

The library also supports sequences of key or key combinations.

myKeys.bind([['Control', 'c'], ['Alt', 'v']], () => {
  // do something
})
// or
myKeys.bind('Control + c > Control + v', () => {
  // do something
})

You Might Be Interested In:


Leave a Reply