hotkey

Type:Function
Status:stable
Since:1.0.0
Platform:js
@blackbyte.sugar.js.keyboard

This function allows you to register a hotkey on the passed element. You can specify the hotkey using the following syntax:

  • ctrl+a
  • shift+ctrl+a
  • a This function take care of avoiding to call your callback when the active element is an input, a textarea or an editable content with the “contenteditable” attribute, unless:
  • the “meta” or “ctrl” key is used in the hotkey.
  • the pressed key is the “escape” key.

Params

  1. key*-String|String[]

    The key(s) to listen for

  2. callback*-Function

    The callback to call when the hotkey is pressed

  3. settings{}THotkeySettings

    Some settings to configure your hotkey

Return

  1. -THotkeyApi

    An object with a cancel method that you can call to cancel the hotkey

Example

import { hotkey } from '@blackbyte/sugar/keyboard'
const hotkeyApi = hotkey('ctrl+a', (e) => {
  console.log('Hotkey pressed');
});

// if you want to cancel the hotkey
hotkeyApi.cancel();

Settings

  1. ctxdocument.bodyHTMLElement

    The context in which to listen for the hotkey

  2. preventDefaulttrueBoolean

    Specify if you want to prevent the default behavior of the hotkey

Todo

  • testsnormal