source: other-projects/nz-flag-design/trunk/render-3d/Flag_files/THREEx.KeyboardState.js@ 29721

Last change on this file since 29721 was 29475, checked in by davidb, 9 years ago

Initial set of files

File size: 3.1 KB
Line 
1// THREEx.KeyboardState.js keep the current state of the keyboard.
2// It is possible to query it at any time. No need of an event.
3// This is particularly convenient in loop driven case, like in
4// 3D demos or games.
5//
6// # Usage
7//
8// **Step 1**: Create the object
9//
10// ```var keyboard = new THREEx.KeyboardState();```
11//
12// **Step 2**: Query the keyboard state
13//
14// This will return true if shift and A are pressed, false otherwise
15//
16// ```keyboard.pressed("shift+A")```
17//
18// **Step 3**: Stop listening to the keyboard
19//
20// ```keyboard.destroy()```
21//
22// NOTE: this library may be nice as standaline. independant from three.js
23// - rename it keyboardForGame
24//
25// # Code
26//
27
28/** @namespace */
29var THREEx = THREEx || {};
30
31/**
32 * - NOTE: it would be quite easy to push event-driven too
33 * - microevent.js for events handling
34 * - in this._onkeyChange, generate a string from the DOM event
35 * - use this as event name
36*/
37THREEx.KeyboardState = function()
38{
39 // to store the current state
40 this.keyCodes = {};
41 this.modifiers = {};
42
43 // create callback to bind/unbind keyboard events
44 var self = this;
45 this._onKeyDown = function(event){ self._onKeyChange(event, true); };
46 this._onKeyUp = function(event){ self._onKeyChange(event, false);};
47
48 // bind keyEvents
49 document.addEventListener("keydown", this._onKeyDown, false);
50 document.addEventListener("keyup", this._onKeyUp, false);
51}
52
53/**
54 * To stop listening of the keyboard events
55*/
56THREEx.KeyboardState.prototype.destroy = function()
57{
58 // unbind keyEvents
59 document.removeEventListener("keydown", this._onKeyDown, false);
60 document.removeEventListener("keyup", this._onKeyUp, false);
61}
62
63THREEx.KeyboardState.MODIFIERS = ['shift', 'ctrl', 'alt', 'meta'];
64THREEx.KeyboardState.ALIAS = {
65 'left' : 37,
66 'up' : 38,
67 'right' : 39,
68 'down' : 40,
69 'space' : 32,
70 'pageup' : 33,
71 'pagedown' : 34,
72 'tab' : 9
73};
74
75/**
76 * to process the keyboard dom event
77*/
78THREEx.KeyboardState.prototype._onKeyChange = function(event, pressed)
79{
80 // log to debug
81 //console.log("onKeyChange", event, pressed, event.keyCode, event.shiftKey, event.ctrlKey, event.altKey, event.metaKey)
82
83 // update this.keyCodes
84 var keyCode = event.keyCode;
85 this.keyCodes[keyCode] = pressed;
86
87 // update this.modifiers
88 this.modifiers['shift']= event.shiftKey;
89 this.modifiers['ctrl'] = event.ctrlKey;
90 this.modifiers['alt'] = event.altKey;
91 this.modifiers['meta'] = event.metaKey;
92}
93
94/**
95 * query keyboard state to know if a key is pressed of not
96 *
97 * @param {String} keyDesc the description of the key. format : modifiers+key e.g shift+A
98 * @returns {Boolean} true if the key is pressed, false otherwise
99*/
100THREEx.KeyboardState.prototype.pressed = function(keyDesc)
101{
102 var keys = keyDesc.split("+");
103 for(var i = 0; i < keys.length; i++){
104 var key = keys[i];
105 var pressed;
106 if( THREEx.KeyboardState.MODIFIERS.indexOf( key ) !== -1 ){
107 pressed = this.modifiers[key];
108 }else if( Object.keys(THREEx.KeyboardState.ALIAS).indexOf( key ) != -1 ){
109 pressed = this.keyCodes[ THREEx.KeyboardState.ALIAS[key] ];
110 }else {
111 pressed = this.keyCodes[key.toUpperCase().charCodeAt(0)]
112 }
113 if( !pressed) return false;
114 };
115 return true;
116}
Note: See TracBrowser for help on using the repository browser.