1.1.0 • Published 8 years ago

kb-handler v1.1.0

Weekly downloads
-
License
WTFPL
Repository
-
Last release
8 years ago

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 nameKeypress supportKeydown supportKeyup 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 NameParametersDescription
pressedNo parametersThe event will only trigger if the origin event is a keyPress
downNo parametersThe event will only trigger if the original event is a keyDown
upNo parametersThe event will only trigger if the original event is a keyUp
onceNo parametersThe event will only trigger once
withAltNo parametersThe event will only trigger if the alt key is down
withCtrlNo parametersThe event will only trigger if the ctrl key is down
withShiftNo parametersThe event will only trigger if the shift key is down
withMetaNo parametersThe event will only trigger if the meta key is down
preventNo parametersPrevents the default behavior of the original event
stopNo parametersStops the propagation of the original event
do0: FunctionApplies a callback to the event (callbacks are called in order which they are assigned