Skip to content

Commit fa69035

Browse files
committed
Finished off Key Combo support. All config options now working correctly.
1 parent 101be29 commit fa69035

6 files changed

Lines changed: 76 additions & 7 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '6393ce50-f872-11e6-ba94-79064404f409'
2+
build: '9d366fd0-f888-11e6-8597-e13c3c71fc73'
33
};
44
module.exports = CHECKSUM;

v3/src/events/Event.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Event = function (type)
22
{
33
this.type = type;
44

5+
// The element that initiated the event.
56
this.target;
67

78
this._propagate = true;

v3/src/input/keyboard/KeyboardManager.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,9 @@ KeyboardManager.prototype = {
167167
}
168168
},
169169

170-
addKeyCombo: function (keys, config)
170+
createCombo: function (keys, config)
171171
{
172-
var combo = new KeyCombo(keys, config);
173-
174-
this.combos.push(combo);
175-
176-
return combo;
172+
return new KeyCombo(this, keys, config);
177173
},
178174

179175
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent

v3/src/input/keyboard/combo/KeyCombo.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
var GetObjectValue = require('../../../utils/object/GetObjectValue');
2+
var ResetKeyCombo = require('./ResetKeyCombo');
3+
var ProcessKeyCombo = require('./ProcessKeyCombo');
4+
var KeyComboMatchEvent = require('./KeyComboMatchEvent');
25

36
// Keys can be either:
47
//
@@ -8,6 +11,8 @@ var GetObjectValue = require('../../../utils/object/GetObjectValue');
811

912
var KeyCombo = function (keyboardManager, keys, config)
1013
{
14+
if (config === undefined) { config = {}; }
15+
1116
// Can't have a zero or single length combo (string or array based)
1217
if (keys.length < 2)
1318
{
@@ -68,13 +73,53 @@ var KeyCombo = function (keyboardManager, keys, config)
6873

6974
// If previously matched and they press Key 1 again, will it reset?
7075
this.resetOnMatch = GetObjectValue(config, 'resetOnMatch', false);
76+
77+
// If the combo matches, will it delete itself?
78+
this.deleteOnMatch = GetObjectValue(config, 'deleteOnMatch', false);
79+
80+
var _this = this;
81+
82+
var onKeyDownHandler = function (event)
83+
{
84+
if (_this.matched || !_this.enabled)
85+
{
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)
96+
{
97+
ResetKeyCombo(_this);
98+
}
99+
else if (_this.deleteOnMatch)
100+
{
101+
_this.destroy();
102+
}
103+
}
104+
};
105+
106+
this.onKeyDown = onKeyDownHandler;
107+
108+
this.manager.events.on('KEY_DOWN_EVENT', onKeyDownHandler);
71109
};
72110

73111
KeyCombo.prototype.constructor = KeyCombo;
74112

75113
KeyCombo.prototype = {
76114

115+
destroy: function ()
116+
{
117+
this.enabled = false;
118+
this.keyCodes = [];
77119

120+
this.manager.events.off('KEY_DOWN', this.onKeyDown);
121+
this.manager = undefined;
122+
}
78123

79124
};
80125

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var Event = require('../../../events/Event');
2+
3+
var KeyComboMatchEvent = function (keyCombo, keyboardEvent)
4+
{
5+
Event.call(this, 'KEY_COMBO_MATCH_EVENT');
6+
7+
this.target = keyCombo;
8+
9+
this.data = keyboardEvent;
10+
};
11+
12+
KeyComboMatchEvent.prototype = Object.create(Event.prototype);
13+
KeyComboMatchEvent.prototype.constructor = KeyComboMatchEvent;
14+
15+
module.exports = KeyComboMatchEvent;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var ResetKeyCombo = function (combo)
2+
{
3+
combo.current = combo.keyCodes[0];
4+
combo.index = 0;
5+
combo.timeLastMatched = 0;
6+
combo.matched = false;
7+
combo.timeMatched = 0;
8+
9+
return combo;
10+
};
11+
12+
module.exports = ResetKeyCombo;

0 commit comments

Comments
 (0)