kb-handler v1.1.0
Kb-handler
var KbHandler = require('kb-handler')
Signature
The keyboard handler constructor needs one parameter which will be the event container. Meaning that all events that occur in this context will be catched.
This container needs to have the prototype method addEventListener
to be working.
Thus all these instanciations are valid:
var kb = new KbHandler(document.body)
var kb = new KbHandler(document.createComment('event container'))
var kb = new KbHandler(window)
Keyboard handler instance
The KbHandler instance exposes a key
property which contains all keys supported.
This is needed for the on
function which registers events on the specified key
kb.on(kb.key.Enter)/* conditions and actions then */
List of supported keys
Key name | Keypress support | Keydown support | Keyup support |
---|---|---|---|
Escape | × | ✔ | ✔ |
F1 | × | ✔ | ✔ |
F2 | × | ✔ | ✔ |
F3 | × | ✔ | ✔ |
F4 | × | ✔ | ✔ |
F5 | × | ✔ | ✔ |
F6 | × | ✔ | ✔ |
F7 | × | ✔ | ✔ |
F8 | × | ✔ | ✔ |
F9 | × | ✔ | ✔ |
F10 | × | ✔ | ✔ |
F11 | × | ✔ | ✔ |
F12 | × | ✔ | ✔ |
PrintScreen | × | × | ✔ |
Backquote | ✔ | ✔ | ✔ |
Digit0 | ✔ | ✔ | ✔ |
Digit1 | ✔ | ✔ | ✔ |
Digit2 | ✔ | ✔ | ✔ |
Digit3 | ✔ | ✔ | ✔ |
Digit4 | ✔ | ✔ | ✔ |
Digit5 | ✔ | ✔ | ✔ |
Digit6 | ✔ | ✔ | ✔ |
Digit7 | ✔ | ✔ | ✔ |
Digit8 | ✔ | ✔ | ✔ |
Digit9 | ✔ | ✔ | ✔ |
Backspace | × | ✔ | ✔ |
Space | ✔ | ✔ | ✔ |
Enter | ✔ | ✔ | ✔ |
Tab | × | ✔ | × |
CapsLock | × | ✔ | ✔ |
ShiftLeft | × | ✔ | ✔ |
ShiftRight | × | ✔ | ✔ |
ControlLeft | × | ✔ | ✔ |
ControlRight | × | ✔ | ✔ |
MetaLeft | × | ✔ | ✔ |
MetaRight | × | ✔ | ✔ |
AltLeft | × | ✔ | ✔ |
AltRight | × | ✔ | ✔ |
Insert | × | ✔ | ✔ |
Home | × | ✔ | ✔ |
Delete | × | ✔ | ✔ |
End | × | ✔ | ✔ |
PageUp | × | ✔ | ✔ |
PageDown | × | ✔ | ✔ |
ArrowLeft | × | ✔ | ✔ |
ArrowRight | × | ✔ | ✔ |
ArrowUp | × | ✔ | ✔ |
ArrowDown | × | ✔ | ✔ |
NumLock | × | × | ✔ |
NumpadDivide | ✔ | ✔ | ✔ |
NumpadMultiply | ✔ | ✔ | ✔ |
NumpadSubstract | ✔ | ✔ | ✔ |
NumpadAdd | ✔ | ✔ | ✔ |
NumpadEnter | ✔ | ✔ | ✔ |
NumpadDecimal | ✔ | ✔ | ✔ |
Numpad0 | ✔ | ✔ | ✔ |
Numpad1 | ✔ | ✔ | ✔ |
Numpad2 | ✔ | ✔ | ✔ |
Numpad3 | ✔ | ✔ | ✔ |
Numpad4 | ✔ | ✔ | ✔ |
Numpad5 | ✔ | ✔ | ✔ |
Numpad6 | ✔ | ✔ | ✔ |
Numpad7 | ✔ | ✔ | ✔ |
Numpad8 | ✔ | ✔ | ✔ |
Numpad9 | ✔ | ✔ | ✔ |
KeyA | ✔ | ✔ | ✔ |
KeyB | ✔ | ✔ | ✔ |
KeyC | ✔ | ✔ | ✔ |
KeyD | ✔ | ✔ | ✔ |
KeyE | ✔ | ✔ | ✔ |
KeyF | ✔ | ✔ | ✔ |
KeyG | ✔ | ✔ | ✔ |
KeyH | ✔ | ✔ | ✔ |
KeyI | ✔ | ✔ | ✔ |
KeyJ | ✔ | ✔ | ✔ |
KeyK | ✔ | ✔ | ✔ |
KeyL | ✔ | ✔ | ✔ |
KeyM | ✔ | ✔ | ✔ |
KeyN | ✔ | ✔ | ✔ |
KeyO | ✔ | ✔ | ✔ |
KeyP | ✔ | ✔ | ✔ |
KeyQ | ✔ | ✔ | ✔ |
KeyR | ✔ | ✔ | ✔ |
KeyS | ✔ | ✔ | ✔ |
KeyT | ✔ | ✔ | ✔ |
KeyU | ✔ | ✔ | ✔ |
KeyV | ✔ | ✔ | ✔ |
KeyW | ✔ | ✔ | ✔ |
KeyX | ✔ | ✔ | ✔ |
KeyY | ✔ | ✔ | ✔ |
KeyZ | ✔ | ✔ | ✔ |
BracketLeft | ✔ | ✔ | ✔ |
BracketRight | ✔ | ✔ | ✔ |
Quote | ✔ | ✔ | ✔ |
Backslash | ✔ | ✔ | ✔ |
Semicolon | ✔ | ✔ | ✔ |
Comma | ✔ | ✔ | ✔ |
Period | ✔ | ✔ | ✔ |
Slash | ✔ | ✔ | ✔ |
Any | ✔ | ✔ | ✔ |
Event methods
Once you register an event, some methods become available, providing you control about the event.
Example:
This event will only trigger when the keyC
will be pressed while holding Control key, will prevent the default
behavior of the original event, will stop its propagation and will call the function pasteMyContent()
before
the function clearMyContentCopied()
.
So this example emulates the true Ctrl+C
paste command.
kb.on(kb.key.keyC).pressed().withCtrl().prevent().stop().do(pasteMyContent).do(clearMyContentCopied)
Mehod Name | Parameters | Description |
---|---|---|
pressed | No parameters | The event will only trigger if the origin event is a keyPress |
down | No parameters | The event will only trigger if the original event is a keyDown |
up | No parameters | The event will only trigger if the original event is a keyUp |
once | No parameters | The event will only trigger once |
withAlt | No parameters | The event will only trigger if the alt key is down |
withCtrl | No parameters | The event will only trigger if the ctrl key is down |
withShift | No parameters | The event will only trigger if the shift key is down |
withMeta | No parameters | The event will only trigger if the meta key is down |
prevent | No parameters | Prevents the default behavior of the original event |
stop | No parameters | Stops the propagation of the original event |
do | 0: Function | Applies a callback to the event (callbacks are called in order which they are assigned |