KeyboardJS 开发指南 - 与 Three.js 配合使用的捕捉键盘组合键库
太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
转载请保留此句:太阳火神的美丽人生 - 本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.
http://robertwhurst.github.com/KeyboardJS/
KeyboardJS is a easy to use keyboard wrapper. It features support for the following:
KeyboardJS.on('a', function() {
console.log('you pressed a!');
});
*** User presses 'a'
>>> 'you pressed a!'
*** User releases 'a'
KeyboardJS.on('ctrl + m', function() {
console.log('ctrl m!');
});
//note the user can press the keys in any order
*** User presses 'ctrl' and 'm'
>>> 'ctrl m!'
*** User releases 'ctrl' and 'm'
KeyboardJS.on('ctrl > m', function() {
console.log('ctrl m!');
});
*** User presses 'ctrl'
*** User presses 'm'
>>> 'ctrl m!'
*** User releases 'ctrl' and 'm'
//if the keys are pressed in the wrong order the binding will not be triggered
*** User presses 'm'
*** User presses 'ctrl'
*** User releases 'm' and 'ctrl'
KeyboardJS.on('ctrl > m', function() {
console.log('ctrl m!');
});
KeyboardJS.on('shift + ctrl > m', function() {
console.log('shift ctrl m!');
});
*** User presses 'ctrl'
*** User presses 'm'
>>> 'ctrl m!'
*** User releases 'ctrl' and 'm'
//note that shift ctrl m does not trigger the ctrl m binding
*** User presses 'shift' and 'ctrl'
*** User presses 'm'
>>> 'shift ctrl m!'
*** User releases 'shift', 'ctrl' and 'm'
KeyboardJS.on(string keyCombo[, function onDownCallback[, function onUpCallback]]) => object binding
Binds any key or key combo. See 'keyCombo' definition below for details. The onDownCallback is fired once the key or key combo becomes active. The onUpCallback is fired when the combo no longer active (a single key is released).
Both the onDownCallback and the onUpCallback are passed three arguments. The first is the key event, the second is the keys pressed, and the third is the key combo string.
Returns an object containing the following methods.
KeyboardJS.activeKeys() => array activeKeys
Returns an array of active keys by name.
KeyboardJS.clear(string keyCombo)
Removes all bindings with the given key combo. See 'keyCombo' definition for more details.
Please note that if you are just trying to remove a single binding should use the clear method in the object returned by KeyboardJS.on instead of this. This function is for removing all binding that use a certain key.
KeyboardJS.clear.key(string keyCombo)
Removes all bindings that use the given key.
KeyboardJS.locale([string localeName]) => string localeName
Changes the locale keyboardJS uses to map key presses. Out of the box KeyboardJS only supports US keyboards, however it is possible to add additional locales via KeyboardJS.locale.register().
KeyboardJS.locale.register(string localeName, object localeDefinition)
Adds new locale definitions to KeyboardJS.
KeyboardJS.macro(string keyCombo, array keyNames)
Accepts a key combo and an array of key names to inject once the key combo is satisfied.
KeyboardJS.macro.remove(string keyCombo)
Accepts a key combo and clears any and all macros bound to that key combo.
KeyboardJS.key.name(number keyCode) => array keyNames
Accepts a key code and returns the key names defined by the current locale.
KeyboardJS.key.code(string keyName) => number keyCode
Accepts a key name and returns the key code defined by the current locale.
KeyboardJS.combo.parse(string keyCombo) => array keyComboArray
Parses a key combo string into a 3 dimensional array.
KeyboardJS.combo.stringify(array keyComboArray) => string KeyCombo
Stringifys a parsed key combo.
A string containing key names separated by whitespace, >
, +
, and ,
.
An object that maps keyNames to their keycode and stores locale specific macros. Used for mapping keys on different locales.
{
"map": {
"65": ["a"],
"66": ["b"],
...
},
"macros": [
["shift + `", ["tilde", "~"]],
["shift + 1", ["exclamation", "!"]],
...
]
}
KeyboardJS can support any locale, however out of the box it just comes with the US locale (for now..,). Adding a new locale is easy. Map your keyboard to an object and pass it to KeyboardJS.locale.register('myLocale', {/localeDefinition/}) then call KeyboardJS.locale('myLocale').
If you create a new locale please consider sending me a pull request or submit it to the issue tracker so I can add it to the library.
I made this to enable better access to key controls in my applications. I'd like to share it with fellow devs. Feel free to fork this project and make your own changes.