Giter Club home page Giter Club logo

hot-key-lib's Introduction

hot-key-lib

pure javascript hot key(keyboard shortcut) library on web

How to use

0. Load hot-key-lib.js

<script src="hot-key-lib.js"></script>

1. Create a HotKey Object

var hotKey = new HotKey();

2. Add HotKey

hotKey.add("CTRL+A", function(e) {
  // do something when CTRL+A keydown
});

Also, you can add HotKey through key array

hotKey.add(["CTRL+A", "META+A"], function(e) {
  // do something when CTRL+A or META+A keydown
});

3. Setup (if you want)

hotKey.setup({
  preventDefault: true
  metaToCtrl: true
});

If you want to know more about Settings, Click Here.

4. Start HotKey

hotKey.start();

5. Stop HotKey

if you want to stop the HotKey, then

hotKey.stop();

Key Name Rule

Default Rule

[CTRL+][META+][SHIFT+][ALT+]KEY

Example

CTRL+A (A+CTRL is not available)
CTRL+SHIFT+A (SHIFT+CTRL+A is not available)
META+A (META is a Command key on Mac)

Use META for Command Key on Mac (or use metaToCtrl)

META+C means Command+C on Mac

Reserved Keys

Key Code Description Shape Name
8 Back Space BACK-SPACE
9 Tab TAB
12 Number Pad 5 without Num Lock NUMPAD-CENTER
13 Enter ENTER
16 Shift SHIFT
17 Ctrl CTRL
18 Alt ALT
19 Pause / Break PAUSE-BREAK
20 Caps Lock CAPS-LOCK
27 ESC ESC
32 Space SPACE
33 Page Up PAGE-UP
34 Page Down PAGE-DOWN
35 End END
36 Home HOME
37 Left Arrow LEFT-ARROW
38 Up Arrow UP-ARROW
39 Right Arrow RIGHT-ARROW
40 Down Arrow DOWN-ARROW
44 Print Screen / Sys Rq PRINT-SCREEN
45 Insert INSERT
46 Delete DELETE
48 ~ 90 0 ~ 9 0 ~ 9
65 ~ 90 A ~ Z A ~ Z
96 ~ 105 Number Pad 0 ~ 9 NUMPAD-0 ~ 9
106 Number Pad * * NUMPAD-*
107 Number Pad + + NUMPAD-*
109 Number Pad - - NUMPAD--
110 Number Pad . . NUMPAD-.
111 Number Pad / / NUMPAD-/
112 ~ 123 F1 ~ F12 F1 ~ F12
144 Num Lock NUM-LOCK
145 Scroll Lock SCROLL-LOCK
186 Semi Colon ; ;
187 Dash - -
188 Equal = =
189 Comma , ,
190 Period . .
191 Slash / /
192 Back Slash \ \
219 Open Bracket [ [
220 Close Bracket ] ]
221 Single Quote ' '
222 Back Quote ` `

Example

hotKey.add("CTRL+SPACE", ...);
hotKey.add("/", ...);
hotKey.add("SHIFT+;", ...);

Special Keys

ALL

hotKey.add("ALL", ...);

This hot key will be call when any key is down. And it doesn't prevent browser's default behavior, even the setting preventDefault is true.

Settings

Name Default Value Description
preventDefault false if true, prevent browser's default behavior
metaToCtrl false if true, change the key META to CTRL.
(So you don't have to add both CTRL+C and META+C hot key for support Windows and Mac)
(just add CTRL+C hot key and setup metaToCtrl to true)
noNumpadNum false if true, change the key NUMPAD-0~9 to 0~9
(So you don't have to add both CTRL+0 and CTRL+NUMPAD-0, only CTRL+0)

Event Parameter

Event Parameter is just KeyboardEvent from Browser. But, we add some data.

Name Description
e.which, e.keyCode the code of key which was down
e.keyString the name of key was down (without CTRL+, META+ and etc like just A)
e.keyFullString the full name of key was down (with CTRL+, META+ and etc like CTRL+A)

License

MIT License Author: WebEngine (TaeSang Cho)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.