A Javascript library that lets you trigger events using keyboard shortcuts.
This library is a ES6+ version of Rick's Shortcuts, I updated it to be used with ES Modules and newer patterns.
Download the shortcut file using via wget:
wget https://raw.githubusercontent.com/VitorGouveia/Shortcut.js/master/shortcuts.js
then, just import it into your file:
import { shortcut } from "./shortcuts.js";
To bind a keyboard shortcut use the add
function:
shortcut.add('ctrl+shift+m', () => {
// your code here
})
To bind a keyboard shortcut to a custom HTML element use the add
function with a third parameter:
const element = document.querySelector("#container")
shortcut.add('ctrl+shift+m', () => {
// your code here
}, element)
To un-bind/remove a keyboard shortcut use the remove
function:
shortcut.remove('ctrl+shift+m')
The following modifier keys are supported:
- cmd (Mac only)
- ctrl
- alt
- shift
see the example for more usage details.
- switch format from IIFE to ESM
- change
var
variables toconst
andlet
- add types with JSDoc
- remove
keycodes
object for nativeevent.key
variable - use object bracket notation to remove
if
's
BSD License
Copyright 2018, Rick Ellis
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.