forked from RobertWHurst/KeyboardJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7997863
Showing
18 changed files
with
2,630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
# Directories | ||
node_modules | ||
|
||
# Editors | ||
.c9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
# Directories | ||
dist | ||
test | ||
|
||
# Editors | ||
.c9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"loadEagerly": [ | ||
"index.js" | ||
], | ||
"plugins": { | ||
"node": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Robert Hurst | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
|
||
KeyboardJS | ||
========== | ||
|
||
[ ![NPM Version](http://img.shields.io/npm/v/keyboardjs.svg?style=flat) ](https://www.npmjs.org/package/keyboardjs) | ||
[ ![Downloads This Week](http://img.shields.io/npm/dm/keyboardjs.svg?style=flat) ](https://www.npmjs.org/package/keyboardjs) | ||
[ ![License](http://img.shields.io/npm/l/keyboardjs.svg?style=flat) ](https://www.npmjs.org/package/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. | ||
|
||
If you're looking for the previous v1.x.x release of KeyboardJS you can find it | ||
[here](https://github.com/RobertWHurst/KeyboardJS/tree/legacy). | ||
|
||
__Setting up bindings is easy__ | ||
|
||
```javascript | ||
keyboardJS.bind('a', function(e) { | ||
console.log('a is pressed'); | ||
}); | ||
keyboardJS.bind('a + b', function(e) { | ||
console.log('a and b is pressed'); | ||
}); | ||
keyboardJS.bind('a + b > c', function(e) { | ||
console.log('a and b then c is pressed'); | ||
}); | ||
keyboardJS.bind(['a + b > c', 'z + y > z'], function(e) { | ||
console.log('a and b then c or z and y then z is pressed'); | ||
}); | ||
|
||
// keyboardJS.bind === keyboardJS.on === keyboardJS.addListener | ||
``` | ||
|
||
|
||
__keydown vs a keyup__ | ||
|
||
```javascript | ||
keyboardJS.bind('a', function(e) { | ||
console.log('a is pressed'); | ||
}, function(e) { | ||
console.log('a is released'); | ||
}); | ||
keyboardJS.bind('a', null, function(e) { | ||
console.log('a is released'); | ||
}); | ||
``` | ||
|
||
|
||
__Prevent keydown repeat__ | ||
|
||
```javascript | ||
keyboardJS.bind('a', function(e) { | ||
// this function will once run once even if a is held | ||
e.preventRepeat(); | ||
console.log('a is pressed'); | ||
}); | ||
``` | ||
|
||
|
||
__Unbind things__ | ||
|
||
```javascript | ||
keyboardJS.unbind('a', previouslyBoundHandler); | ||
// keyboardJS.unbind === keyboardJS.off === keyboardJS.removeListener | ||
``` | ||
|
||
|
||
__Using contexts__ | ||
|
||
```javascript | ||
|
||
// these will execute in all contexts | ||
keyboardJS.bind('a', function(e) {}); | ||
keyboardJS.bind('b', function(e) {}); | ||
keyboardJS.bind('c', function(e) {}); | ||
|
||
// these will execute in the index context | ||
keyboardJS.setContext('index'); | ||
keyboardJS.bind('1', function(e) {}); | ||
keyboardJS.bind('2', function(e) {}); | ||
keyboardJS.bind('3', function(e) {}); | ||
|
||
// these will execute in the foo context | ||
keyboardJS.setContext('foo'); | ||
keyboardJS.bind('x', function(e) {}); | ||
keyboardJS.bind('y', function(e) {}); | ||
keyboardJS.bind('z', function(e) {}); | ||
|
||
// if we have a router we can activate these contexts when appropriate | ||
myRouter.on('GET /', function(e) { | ||
keyboardJS.setContext('index'); | ||
}); | ||
myRouter.on('GET /foo', function(e) { | ||
keyboardJS.setContext('foo'); | ||
}); | ||
|
||
// you can always figure out your context too | ||
var contextName = keyboardJS.getContext(); | ||
``` | ||
|
||
|
||
__pause, resume, and reset__ | ||
|
||
```javascript | ||
|
||
// the keyboard will no longer trigger bindings | ||
keyboardJS.pause(); | ||
|
||
// the keyboard will once again trigger bindings | ||
keyboardJS.resume(); | ||
|
||
// all active bindings will released and unbound, | ||
// pressed keys will be cleared | ||
keyboardJS.reset(); | ||
``` | ||
|
||
|
||
__pressKey, releaseKey, and releaseAllKeys__ | ||
|
||
```javascript | ||
|
||
// pressKey | ||
keyboardJS.pressKey('a'); | ||
// or | ||
keyboardJS.pressKey(65); | ||
|
||
// releaseKey | ||
keyboardJS.releaseKey('a'); | ||
// or | ||
keyboardJS.releaseKey(65); | ||
|
||
// releaseAllKeys | ||
keyboardJS.releaseAllKeys(); | ||
``` | ||
|
||
|
||
__watch and stop__ | ||
|
||
```javascript | ||
// bind to the window and document in the current window | ||
keyboardJS.watch(); | ||
|
||
// or pass your own window and document | ||
keyboardJS.watch(myDoc); | ||
keyboardJS.watch(myWin, myDoc); | ||
|
||
// or scope to a specific element | ||
keyboardJS.watch(myForm); | ||
keyboardJS.watch(myWin, myForm); | ||
|
||
// detach KeyboardJS from the window and document/element | ||
keyboardJS.stop(); | ||
``` |
Oops, something went wrong.