KeyboardJS
KeyboardJS is a library for use in the browser (node.js compatible). It Allows developers to easily setup key bindings. Use key combos to setup complex bindings. KeyboardJS also provides contexts. Contexts are great for single page applications. They allow you to scope your bindings to various parts of your application. Out of the box keyboardJS uses a US keyboard locale. If you need support for a different type of keyboard KeyboardJS provides custom locale support so you can create with a locale that better matches your needs.
KeyboardJS is available as a NPM module. If you're not using a build system like webpack, simply add keyboard.js or keyboard.min.js from the dist folder in this repo to your project via a script tag.
npm install keyboardjs
Note that all key names can be found in ./locales/us.js.
Setting up bindings is easy
keyboardJS;keyboardJS;keyboardJS;keyboardJS;keyboardJS;//alt, shift, ctrl, etc must be lowercasekeyboardJS; // keyboardJS.bind === keyboardJS.on === keyboardJS.addListener
keydown vs a keyup
keyboardJS;keyboardJS;
Prevent keydown repeat
keyboardJS;
Unbind things
keyboardJS;// keyboardJS.unbind === keyboardJS.off === keyboardJS.removeListener
Using contexts
// these will execute in all contexts keyboardJS; keyboardJS; keyboardJS; // these will execute in the index context keyboardJS; keyboardJS; keyboardJS; keyboardJS; // these will execute in the foo context keyboardJS; keyboardJS; keyboardJS; keyboardJS; // if we have a router we can activate these contexts when appropriate myRouter; myRouter; // you can always figure out your context too const contextName = keyboardJS; // you can also set up handlers for a context without losing the current context keyboardJS;
pause, resume, and reset
// the keyboard will no longer trigger bindingskeyboardJS; // the keyboard will once again trigger bindingskeyboardJS; // all active bindings will released and unbound,// pressed keys will be clearedkeyboardJS;
pressKey, releaseKey, and releaseAllKeys
// pressKeykeyboardJS;// orkeyboardJS; // releaseKeykeyboardJS;// orkeyboardJS; // releaseAllKeyskeyboardJS;
watch and stop
// bind to the window and document in the current windowkeyboardJS; // or pass your own window and documentkeyboardJS;keyboardJS; // or scope to a specific elementkeyboardJS;keyboardJS; // detach KeyboardJS from the window and document/elementkeyboardJS;