Shortcut(optionsopt) → {object}

new Shortcut(optionsopt) → {object}

JavaScript keyboard shortcut singleton to handle key strokes

The Shortcut class provides a singleton object to set custom keyboard shortcuts for web application. It allows to register key shortcuts using a string notation (ie. Ctrl+A). It also exposes several methods to pause/resume the callback for a given event or for all of them. With the key string, you must provide a callback function to react to this shortcut fire event. That's all folks! For source code, please go to Shortcut.js
Parameters:
Name Type Attributes Default Description
options object <optional>
{}

The Shortcut singleton options, not mandatory

Properties
Name Type Attributes Default Description
keyEvent string <optional>
keydown

The key event to react from

autoRepeat boolean <optional>
true

For keydown and keypress, auto repeat event if key is held on pushed

Returns:
object -
  • The Shortcut singleton instance
Since:
  • September 2020
Author:
  • Arthur Beaulieu

Members

private _autoRepeat :boolean

The auto repeat of an event when key is held on push

private _keyEvent :string

Key event to use on keyboard event listener

private _multiKey :Array.<object>

Multi keys saved shortcuts

private _noPrevention :boolean

Do not call prevent default on key event flag

private _singleKey :Array.<object>

Single key saved shortcuts

version :string

Component version

Methods

private, static _addEvents()

Internal private method to subscribe to DOM key stroke event, depending on which key event was set in constructor, or using the updateKeyEvent() method. The key event is then calling back the _testShortcuts() method to check if any event has to be fired.

private, static _getModifiersCount(keyString)

Shorthand method to count modifiers in a given shrotcut string. It uses regex that are case insensitive to avoid multi testing (and because it's what this singleton do).
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

private, static _multiKeyEvent(event)

This method will parse all multi keys events registered in its internal attributes, and will fire the call back if its registered key matches the event key. Multi key events are made using ctrl, alt and shift modifiers. It also prevent defaults on the event only if a match is found to keep browser behavior in case there is no regstered shortcut.
Parameters:
Name Type Description
event event

The keyboard event (keydown, keypress and keyup)

private, static _removeEvents()

Internal private method to revoke DOM subscribtion to a key stroke event, depending on which key event was set in constructor, or using the updateKeyEvent() method. The key event won't then call back the _testShortcuts() method.

private, static _setAllPauseFlag(value)

Parse all registered event and make them listen or not to any key event.
Parameters:
Name Type Description
value boolean

The state to set, to pause/resume all registered shortcuts

private, static _setOnePauseFlag(keyString, value)

Parse all registered event and make the one that matches the key string listen or not to any key event.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

value boolean

The state to set, to pause/resume all registered shortcuts

private, static _shortcutAlreadyExist(keyString) → {boolean}

Internal method to test if a given key string isn't already related to a registered shortcut.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

Returns:
boolean -
  • The existence state of given key string

private, static _singleKeyEvent(event)

This method will parse all single key events registered in its internal attributes, and will fire the call back if its registered key matches the event key. It also prevent defaults on the event only if a match is found to keep browser behavior in case there is no regstered shortcut.
Parameters:
Name Type Description
event event

The keyboard event (keydown, keypress and keyup)

private, static _testShortcuts(event)

This method needs to be called when a key event has been detected. It takes as a parameters the JavaScript event object, which contains several, required, information. It will then crawl the registered shortcuts to check if one needs to be fired and call back the application. It handle both single key and multi key shortcuts. Finally, it will not fire any event if the event repeat flag is at true, and the singleton is not in auto repeat event.
Parameters:
Name Type Description
event event

The keyboard event (keydown, keypress and keyup)

static destroy()

Shortcut destructor. Will delete singleton instance, its events and its properties.

static pause(keyString)

This method will pause a registered shortcut using its key string. The shortcut won't then fire the callback when the shortcut is used.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

static pauseAll(keyString)

This method will pause all registered shortcuts.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

static register(keyString, fire)

This method is the entry point to register a shortcut. The caller must send the key comibination as a string (ie. F, Ctrl+shift+r). The Shortcut singleton is case insensitive, which means you can write it with the case you want. For modifiers, please use ctrl, alt and shift strings. Then the given callback will be called each time the key stroke matches the shortcut key string. For fine tuning on auto repeat, see constructor options.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

fire function

The callback to attach to the shortcut

static remove(keyString)

This method will remove a registered shortcut using its key string.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

static removeAll()

This method will remove all registered shortcut events.

static resume(keyString)

This method will resume a registered shortcut using its key string. The shortcut will then fire the callback when the shortcut is used.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

static resumeAll(keyString)

This method will resume all registered shortcuts.
Parameters:
Name Type Description
keyString string

The raw shortcut string that is defined when registering an event

static updateAutoRepeat(autoRepeat)

This method will update the auto repeat flag that makes an event continuously callback when the key is hed pushed.
Parameters:
Name Type Description
autoRepeat boolean

The auto repeat state to set

static updateKeyEvent(keyEvent)

This method will update the singleton's listener for a given keyboard event.
Parameters:
Name Type Description
keyEvent string

The key event to apply to the DOM listener in keydown, keypress and keyup