Capture Keyboard Input And Trigger A Function On Press – shortcut.js

Category: Javascript | May 3, 2021
Author:houdinii
Views Total:241 views
Official Page:Go to website
Last Update:May 3, 2021
License:MIT

Preview:

Capture Keyboard Input And Trigger A Function On Press – shortcut.js

Description:

shortcut.js is a lightweight (4kb minified) keyboard shortcut (hotkey) JavaScript library that allows you to bind keyboard combos and key presses.

How to use it:

1. Insert the shortcut.min.js JavaScript library into your page.

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

2. Create a shortcut and assign it a function to call on press.

shortcut.add("f5", function doKeyCheck() {
  // do something
});
shortcut.add("enter", function doKeyCheck() {
  // do something
});

3. Modifier keys are supported as well.

shortcut.add("Ctrl+A", function doKeyCheck() {
  // do something
});
shortcut.add("Ctrl+C", function doKeyCheck() {
  // do something
});

4. Customize the trigger event (‘keydown’,’keyup’, or ‘keypress’). Default: ‘keydown’.

shortcut.add("Ctrl+A", function doKeyCheck() {
  // do something
},{
  'type':'keypress'
});

5. Determine whether to disable the library on text fields like input and textarea. Default: false.

shortcut.add("Ctrl+A", function doKeyCheck() {
  // do something
},{
  'disable_in_input': true
});

6. Determine the DOM element in which the library watches for the keyboard event.

shortcut.add("Ctrl+A", function doKeyCheck() {
  // do something
},{
  'target': document
});

7. Determine whether to use keycode instead. Default: false.

shortcut.add("65", function doKeyCheck() {
  // do something
},{
  'keycode': true
});

8. Determine whether to allow the event to propagate. Default: false.

shortcut.add("65", function doKeyCheck() {
  // do something
},{
  'propagate': true
});

9. Remove a shortcut.

shortcut.remove(keyCombo)

You Might Be Interested In:


Leave a Reply