Keyboard shortcut handler. KISS, light and library free.

Overview

Shortcut.js

License

Shortcut.js is a JavaScript ES6 component that offers a global keyboard event handler. This way one can declare and react to any combination of keys. It also offers several useful methods to manipulates those registered keyboard events.

With ~5Ko minified, Shortcut.js is designed to be stable and remain as light as possible. It is meant to be used application wide so all your components can react to keyboard events.

Get Started

This repository was made to store documentation, test bench and source code. If you want to include this component in your project, you either need the src/Shortcut.js file if you have an assets bundler in your project, or use the dist/Shortcut.min.js to use the minified component. This minified file is compiled in ES5 JavaScript for compatibility reasons. The unminified file is, in the contrary, coded in ES6 JavaScript.

Here's an example on how to create a custom event handler :

/* Import the Js component */
import Shortcut from 'src/Shortcut.js';
/* Create a basic shortcut handler */
const myShortcutHandler = new Shortcut();
/* ...Or, you can provide several options to customize the handler */
const myShortcutHandler = new Shortcut({
  keyEvent: 'keydown', // 'keydown' or 'keyup' or 'keypressed'
  autoRepeat: true // If key is kept pushed, do event have to repeat ?
});

If you want to update the shortcut handler options, you can use updateKeyEvent(string) or updateAutoRepeat(boolean). Refer to the documentation for allowed values.

The handler is now ready to register and react to keyboard events.

Register a keyboard event

To register a keyboard event, you must call the register method and give it a string for the key to listen (case insensitive), and a callback to fire each time it is pressed.

myShortcutHandler.register('F', () => {
  alert('Pay respect');
});

When declaring a multi key event, all modifiers must be placed before the actual key, meaning the actual key is the last character of the string (also case insensitive). The + char in the following example is used for clarity but is not mandatory. One can use the following modifiers : ctrl, alt, shift. There is no modifiers order to respect.

myShortcutHandler.register('Ctrl+Alt+Shift+F', () => {
  alert('Pay triple respect');
});

If you want to register an event with a key that is neither a letter or a number, you must use the event.key value. For references, use the MDN sandbox so you can find the event.key for your shortcut.

Manipulate a keyboard event

You can also control those events to pause or resume them on demand.

// Pause a keyboard event using its definition string
myShortcutHandler.pause('F');
myShortcutHandler.pause('Ctrl+Alt+Shift+F');
// Resume it using its definition string
myShortcutHandler.resume('F');
myShortcutHandler.resume('Ctrl+Alt+Shift+F');
// ...Or use the global pause/resume method
myShortcutHandler.pauseAll();
myShortcutHandler.resumeAll();

To remove the listener of this keyboard event, simply use the following methods.

// Remove a keyboard event using its definition string
myShortcutHandler.remove('Ctrl+Alt+Shift+F');
// ...Or, remove all subscribed keyboard events
myShortcutHandler.removeAll();

You're now good to go! If however you need more information, you can read the online documentation.

Development

If you clone this repository, you can npm install to install development dependencies. This will allow you to build dist file, run the component tests or generate the documentation ;

  • npm run build to generate the minified file ;
  • npm run dev to watch for any change in source code ;
  • npm run web-server to launch a local development server ;
  • npm run doc to generate documentation ;
  • npm run test to perform all tests at once ;
  • npm run testdev to keep browsers open to debug tests ;
  • npm run beforecommit to perform tests, generate doc and bundle the JavaScript.

To avoid CORS when locally loading the example HTML file, run the web server. Please do not use it on a production environment. Unit tests are performed on both Firefox and Chrome ; ensure you have both installed before running tests, otherwise they might fail.

If you have any question or idea, feel free to DM or open an issue (or even a PR, who knows) ! I'll be glad to answer your request.

You might also like...

Error and Success Handler for Passport.js

Error and Success Handler for Passport.js

passport-handler Developed with ❤️ by Swôth 🔎 Installation npm i passport-handler --save yarn add passport-handler 🔮 Importing import Handler from '

Oct 29, 2022

New Discord.JS v14 Slash and Prefix Commands handler with Events. Check it out now!

New Discord.JS v14 Slash and Prefix Commands handler with Events. Check it out now!

Discord.js v14 Command-Handler Commands, Events, Permissions and Cooldown Handlers for Discord.js v14 bot ~ Made by Lynx Discord.js v14 (dev version)

Dec 19, 2022

A Discord bot Template made with Discord.JS version 14 includes Prefix, Slash commands and MongoDB handler.

A Discord bot Template made with Discord.JS version 14 includes Prefix, Slash commands and MongoDB handler.

Project Language: Fork/Download for: Project Requirements: Database Available: DiscordJS V14 Bot Template - Introduction: A Discord bot project made w

Jan 3, 2023

🌗 1 line of code to apply auto dark / light theme and support custom theme for your website. Super fast and lightweight theme library.

themes.js A super lightweight and fast Theme library with auto system color scheme detection in JavaScript. Features Auto detect Dark / Light mode by

Nov 29, 2022

Fancytree - JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading

Fancytree - JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkboxes, drag'n'drop, and lazy loading

Fancytree Fancytree (sequel of DynaTree 1.x) is a JavaScript tree view / tree grid plugin with support for keyboard, inline editing, filtering, checkb

Jan 9, 2023

As babies smash on the keyboard, images, letters and numbers appear on the screen

As babies smash on the keyboard, images, letters and numbers appear on the screen

Baby Bam Bam As babies smash on the keyboard, images, letters and numbers appear on the screen. Try the many options and find the ones that are right

Oct 24, 2022

Preview and reply to your messages from within Alfred. Keep your hands on that keyboard!

Preview and reply to your messages from within Alfred. Keep your hands on that keyboard!

Mouseless Messenger Mouseless Messenger offers you rapid keyboard-driven access to send and read conversations in the macOS Messages app via Alfred —

Dec 30, 2022

Canvas-based JavaScript UI element implementing touch, keyboard, mouse and scroll wheel support.

Canvas-based JavaScript UI element implementing touch, keyboard, mouse and scroll wheel support.

pure-knob Initially a (circular) knob / dial control with mouse, wheel, touch and keyboard support, implemented in pure JavaScript. In addition, this

Nov 4, 2022

An Amazing SlashCommands Handler (With Sharding & Mongo) Made by discord.gg/azury

An Amazing SlashCommands Handler (With Sharding & Mongo) Made by discord.gg/azury

SlashCommands Handler by The Azury Team If this Git-Repo gets "40" Stars ⭐ i'll add some more Commands! 🛠️ FEATURES: 1. SlashCommands Support 2. Cont

Dec 2, 2022
Releases(1.0.0)
Owner
Arthur Beaulieu
Web UI/UX engineer – Paris 🇫🇷
Arthur Beaulieu
A lightweight (~850 B) library for easy mac/window shortcut notation. kbd-txt convert shortcut text depending on the type of OS (window/linux/mac).

kbd-txt A lightweight (~850 B) library for easy mac/window shortcut notation. kbd-txt convert shortcut text depending on the type of OS (window/linux/

Minung Han 6 Jan 1, 2023
Unified JavaScript logging system. KISS, light and library free.

Logger.js Logger.js is a JavaScript ES6 module that offers a unified console output across Firefox and Chromium based browsers. It handles standard er

Arthur Beaulieu 1 Oct 1, 2020
DiscordJs13-handler is the solution for all the discord.js developper who want a complete handler for slash commands

DiscordJs13-handler is the solution for all the discord.js developper who want a complete handler for slash commands

null 9 Jul 2, 2022
In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. I followed the JavaScript, CSS, HTML, DRY, KISS and YAGNI Best practices.

To Do list: add & remove In this project, I implement a Simple To Do List with the CRUD (create, read, update, delete) methods. All the elements of th

Olivier 6 Nov 20, 2022
Long-press `⌘` key or press `?` to present a shortcut guide for your Web application.

React Shortcut Guide Status: Alpha Long-press command or press ? to present a shortcut guide for your Web application. Gzip+minify < 3kB Install npm i

Reactify Component 9 Jul 29, 2022
CSS sprite to represent iOS shortcut icons

shortcut-icons A CSS sprite to represent iOS shortcut icons. Grab the stylesheet and the default (iOS 15) glyph set, and add a shortcut icon to your H

Antonio Bueno 18 Nov 14, 2022
A Deno library for interacting with the mouse 🖱️ keyboard ⌨️ and screen 💻

A Deno library for interacting with the mouse ??️ keyboard ⌨️ and screen ?? . Litebot provides a simple API for creating kbm events, macros, & working with displays. Litebot leverages Deno's FFI to allow speedy low level control in C & C++ while having a typescript API exposed to the user.

Tyler Laceby 10 Aug 30, 2022
A simple library for handling keyboard shortcuts with Alpine.js.

✨ Help support the maintenance of this package by sponsoring me. Alpine.js Mousetrap A simple library for handling keyboard shortcuts with Alpine.js.

Dan Harrin 102 Nov 14, 2022
➷ A robust Javascript library for capturing keyboard input. It has no dependencies.

Hotkeys HotKeys.js is an input capture library with some very special features, it is easy to pick up and use, has a reasonable footprint (~3kb) (gzip

小弟调调™ 5.7k Jan 4, 2023
A Virtual Interactive Keyboard which replicates every key you press and a Text-Area in which everything is written and can be copied to the clipboard with a click of a button.

A Virtual Interactive Keyboard which replicates every key you press and a Text-Area in which everything is written and can be copied to the clipboard with a click of a button.

Devang Joshi 1 Mar 1, 2021