天天看點

KeyboardJS 開發指南 - 與 Three.js 配合使用的捕捉鍵盤組合鍵庫KeyboardJS

KeyboardJS 開發指南 - 與 Three.js 配合使用的捕捉鍵盤組合鍵庫

<a target="_blank" href="http://blog.csdn.net/opengl_es">轉載請保留此句:太陽火神的美麗人生 -  本部落格專注于 靈活開發及移動和物聯裝置研究:iOS、Android、Html5、Arduino、pcDuino,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。</a>

A JavaScript library for binding keyboard combos without the pain of key codes and key combo conflicts.

<a target="_blank" href="http://robertwhurst.github.com/KeyboardJS/">http://robertwhurst.github.com/KeyboardJS/</a>

KeyboardJS 開發指南 - 與 Three.js 配合使用的捕捉鍵盤組合鍵庫KeyboardJS

KeyboardJS is a easy to use keyboard wrapper. It features support for the following:

Advanced key combos - Support for advanced combos with ordered stages.

Key combo overlap prevention - Prevents against bindings with shorter combos firing when another binding with a longer combo, sharing the same keys, has already been executed.

Macro keys - Support for adding vurtual keys backed by a key combo instead of a physical key.

Keyboard locales - Support for multiple locales. Comes with US locale.

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.

clear() - Removes the key or key combo binding.

on() - Allows you to bind to the keyup and keydown event of the given combo. An alternative to adding the onDownCallback and onUpCallback.

Returns an array of active keys by name.

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.

Removes all bindings that use the given key.

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().

Adds new locale definitions to KeyboardJS.

Accepts a key combo and an array of key names to inject once the key combo is satisfied.

Accepts a key combo and clears any and all macros bound to that key combo.

Accepts a key code and returns the key names defined by the current locale.

Accepts a key name and returns the key code defined by the current locale.

Parses a key combo string into a 3 dimensional array.

Level 1 - sub combos.

Level 2 - combo stages. A stage is a set of key name pairs that must be satisfied in the order they are defined.

Level 3 - each key name to the stage.

Stringifys a parsed key combo.

A string containing key names separated by whitespace, <code>&gt;</code>, <code>+</code>, and <code>,</code>.

'a' - binds to the 'a' key. Pressing 'a' will match this keyCombo.

'a, b' - binds to the 'a' and 'b' keys. Pressing either of these keys will match this keyCombo.

'a + b' - binds to the 'a' and 'b' keys. Pressing both of these keys will match this keyCombo.

'a + b, c + d' - binds to the 'a', 'b', 'c' and 'd' keys. Pressing either the 'a' key and the 'b' key, or the 'c' and the 'd' key will match this keyCombo.

An object that maps keyNames to their keycode and stores locale specific macros. Used for mapping keys on different locales.

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').

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.

繼續閱讀