1- var GetValue = require ( '../../../utils/object/GetValue' ) ;
2- var ResetKeyCombo = require ( './ResetKeyCombo' ) ;
3- var ProcessKeyCombo = require ( './ProcessKeyCombo' ) ;
1+ var Class = require ( '../../../utils/Class' ) ;
2+ var GetFastValue = require ( '../../../utils/object/GetFastValue' ) ;
43var KeyComboMatchEvent = require ( './KeyComboMatchEvent' ) ;
4+ var ProcessKeyCombo = require ( './ProcessKeyCombo' ) ;
5+ var ResetKeyCombo = require ( './ResetKeyCombo' ) ;
56
67// Keys can be either:
78//
89// A string (ATARI)
910// An array of either integers (key codes) or strings, or a mixture of both
1011// An array of objects (such as Key objects) with a public 'keyCode' property
1112
12- var KeyCombo = function ( keyboardManager , keys , config )
13- {
14- if ( config === undefined ) { config = { } ; }
13+ var KeyCombo = new Class ( {
1514
16- // Can't have a zero or single length combo (string or array based)
17- if ( keys . length < 2 )
15+ initialize :
16+
17+ function KeyCombo ( keyboardManager , keys , config )
1818 {
19- return false ;
20- }
19+ if ( config === undefined ) { config = { } ; }
2120
22- this . manager = keyboardManager ;
21+ // Can't have a zero or single length combo (string or array based)
22+ if ( keys . length < 2 )
23+ {
24+ return false ;
25+ }
2326
24- this . enabled = true ;
27+ this . manager = keyboardManager ;
2528
26- this . keyCodes = [ ] ;
29+ this . enabled = true ;
2730
28- // if 'keys' is a string we need to get the keycode of each character in it
31+ this . keyCodes = [ ] ;
2932
30- for ( var i = 0 ; i < keys . length ; i ++ )
31- {
32- var char = keys [ i ] ;
33+ // if 'keys' is a string we need to get the keycode of each character in it
3334
34- if ( typeof char === 'string' )
35- {
36- this . keyCodes . push ( char . toUpperCase ( ) . charCodeAt ( 0 ) ) ;
37- }
38- else if ( typeof char === 'number' )
39- {
40- this . keyCodes . push ( char ) ;
41- }
42- else if ( char . hasOwnProperty ( 'keyCode' ) )
35+ for ( var i = 0 ; i < keys . length ; i ++ )
4336 {
44- this . keyCodes . push ( char . keyCode ) ;
37+ var char = keys [ i ] ;
38+
39+ if ( typeof char === 'string' )
40+ {
41+ this . keyCodes . push ( char . toUpperCase ( ) . charCodeAt ( 0 ) ) ;
42+ }
43+ else if ( typeof char === 'number' )
44+ {
45+ this . keyCodes . push ( char ) ;
46+ }
47+ else if ( char . hasOwnProperty ( 'keyCode' ) )
48+ {
49+ this . keyCodes . push ( char . keyCode ) ;
50+ }
4551 }
46- }
4752
48- // The current keyCode the combo is waiting for
49- this . current = this . keyCodes [ 0 ] ;
53+ // The current keyCode the combo is waiting for
54+ this . current = this . keyCodes [ 0 ] ;
5055
51- // The current index of the key being waited for in the 'keys' string
52- this . index = 0 ;
56+ // The current index of the key being waited for in the 'keys' string
57+ this . index = 0 ;
5358
54- // The length of this combo (in keycodes)
55- this . size = this . keyCodes . length ;
59+ // The length of this combo (in keycodes)
60+ this . size = this . keyCodes . length ;
5661
57- // The time the previous key in the combo was matched
58- this . timeLastMatched = 0 ;
62+ // The time the previous key in the combo was matched
63+ this . timeLastMatched = 0 ;
5964
60- // Has this Key Combo been matched yet?
61- this . matched = false ;
65+ // Has this Key Combo been matched yet?
66+ this . matched = false ;
6267
63- // The time the entire combo was matched
64- this . timeMatched = 0 ;
68+ // The time the entire combo was matched
69+ this . timeMatched = 0 ;
6570
66- // Custom options ...
71+ // Custom options ...
6772
68- // If they press the wrong key do we reset the combo?
69- this . resetOnWrongKey = GetValue ( config , 'resetOnWrongKey' , true ) ;
73+ // If they press the wrong key do we reset the combo?
74+ this . resetOnWrongKey = GetFastValue ( config , 'resetOnWrongKey' , true ) ;
7075
71- // The max delay in ms between each key press. Above this the combo is reset. 0 means disabled.
72- this . maxKeyDelay = GetValue ( config , 'maxKeyDelay' , 0 ) ;
76+ // The max delay in ms between each key press. Above this the combo is reset. 0 means disabled.
77+ this . maxKeyDelay = GetFastValue ( config , 'maxKeyDelay' , 0 ) ;
7378
74- // If previously matched and they press Key 1 again, will it reset?
75- this . resetOnMatch = GetValue ( config , 'resetOnMatch' , false ) ;
79+ // If previously matched and they press Key 1 again, will it reset?
80+ this . resetOnMatch = GetFastValue ( config , 'resetOnMatch' , false ) ;
7681
77- // If the combo matches, will it delete itself?
78- this . deleteOnMatch = GetValue ( config , 'deleteOnMatch' , false ) ;
82+ // If the combo matches, will it delete itself?
83+ this . deleteOnMatch = GetFastValue ( config , 'deleteOnMatch' , false ) ;
7984
80- var _this = this ;
85+ var _this = this ;
8186
82- var onKeyDownHandler = function ( event )
83- {
84- if ( _this . matched || ! _this . enabled )
87+ var onKeyDownHandler = function ( event )
8588 {
86- return ;
87- }
88-
89- var matched = ProcessKeyCombo ( event . data , _this ) ;
90-
91- if ( matched )
92- {
93- _this . manager . events . dispatch ( new KeyComboMatchEvent ( _this , event ) ) ;
94-
95- if ( _this . resetOnMatch )
89+ if ( _this . matched || ! _this . enabled )
9690 {
97- ResetKeyCombo ( _this ) ;
91+ return ;
9892 }
99- else if ( _this . deleteOnMatch )
93+
94+ var matched = ProcessKeyCombo ( event . data , _this ) ;
95+
96+ if ( matched )
10097 {
101- _this . destroy ( ) ;
98+ _this . manager . events . dispatch ( new KeyComboMatchEvent ( _this , event ) ) ;
99+
100+ if ( _this . resetOnMatch )
101+ {
102+ ResetKeyCombo ( _this ) ;
103+ }
104+ else if ( _this . deleteOnMatch )
105+ {
106+ _this . destroy ( ) ;
107+ }
102108 }
103- }
104- } ;
109+ } ;
105110
106- this . onKeyDown = onKeyDownHandler ;
111+ this . onKeyDown = onKeyDownHandler ;
107112
108- this . manager . events . on ( 'KEY_DOWN_EVENT' , onKeyDownHandler ) ;
109- } ;
113+ this . manager . events . on ( 'KEY_DOWN_EVENT' , onKeyDownHandler ) ;
114+ } ,
110115
111- KeyCombo . prototype . constructor = KeyCombo ;
116+ progress : {
112117
113- KeyCombo . prototype = {
118+ // How far complete is this combo? A value between 0 and 1.
119+ get : function ( )
120+ {
121+ return this . index / this . size ;
122+ }
123+
124+ } ,
114125
115126 destroy : function ( )
116127 {
@@ -121,22 +132,6 @@ KeyCombo.prototype = {
121132 this . manager = undefined ;
122133 }
123134
124- } ;
125-
126- Object . defineProperties ( KeyCombo . prototype , {
127-
128- progress : {
129-
130- enumerable : true ,
131-
132- // How far complete is this combo? A value between 0 and 1.
133- get : function ( )
134- {
135- return this . index / this . size ;
136- }
137-
138- }
139-
140135} ) ;
141136
142137module . exports = KeyCombo ;
0 commit comments