From fa69035e4fd02c6c3cd6da8a9dffb497b9b45716 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 21 Feb 2017 23:01:05 +0000 Subject: [PATCH] Finished off Key Combo support. All config options now working correctly. --- v3/src/checksum.js | 2 +- v3/src/events/Event.js | 1 + v3/src/input/keyboard/KeyboardManager.js | 8 +--- v3/src/input/keyboard/combo/KeyCombo.js | 45 +++++++++++++++++++ .../keyboard/combo/KeyComboMatchEvent.js | 15 +++++++ v3/src/input/keyboard/combo/ResetKeyCombo.js | 12 +++++ 6 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 v3/src/input/keyboard/combo/KeyComboMatchEvent.js create mode 100644 v3/src/input/keyboard/combo/ResetKeyCombo.js diff --git a/v3/src/checksum.js b/v3/src/checksum.js index fa03f1a17..ac76f4ba5 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '6393ce50-f872-11e6-ba94-79064404f409' +build: '9d366fd0-f888-11e6-8597-e13c3c71fc73' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/events/Event.js b/v3/src/events/Event.js index b51ea674e..23681d636 100644 --- a/v3/src/events/Event.js +++ b/v3/src/events/Event.js @@ -2,6 +2,7 @@ var Event = function (type) { this.type = type; + // The element that initiated the event. this.target; this._propagate = true; diff --git a/v3/src/input/keyboard/KeyboardManager.js b/v3/src/input/keyboard/KeyboardManager.js index 859f856e6..9b9bdb5db 100644 --- a/v3/src/input/keyboard/KeyboardManager.js +++ b/v3/src/input/keyboard/KeyboardManager.js @@ -167,13 +167,9 @@ KeyboardManager.prototype = { } }, - addKeyCombo: function (keys, config) + createCombo: function (keys, config) { - var combo = new KeyCombo(keys, config); - - this.combos.push(combo); - - return combo; + return new KeyCombo(this, keys, config); }, // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent diff --git a/v3/src/input/keyboard/combo/KeyCombo.js b/v3/src/input/keyboard/combo/KeyCombo.js index 9cefcb8ce..d4192ddb1 100644 --- a/v3/src/input/keyboard/combo/KeyCombo.js +++ b/v3/src/input/keyboard/combo/KeyCombo.js @@ -1,4 +1,7 @@ var GetObjectValue = require('../../../utils/object/GetObjectValue'); +var ResetKeyCombo = require('./ResetKeyCombo'); +var ProcessKeyCombo = require('./ProcessKeyCombo'); +var KeyComboMatchEvent = require('./KeyComboMatchEvent'); // Keys can be either: // @@ -8,6 +11,8 @@ var GetObjectValue = require('../../../utils/object/GetObjectValue'); var KeyCombo = function (keyboardManager, keys, config) { + if (config === undefined) { config = {}; } + // Can't have a zero or single length combo (string or array based) if (keys.length < 2) { @@ -68,13 +73,53 @@ var KeyCombo = function (keyboardManager, keys, config) // If previously matched and they press Key 1 again, will it reset? this.resetOnMatch = GetObjectValue(config, 'resetOnMatch', false); + + // If the combo matches, will it delete itself? + this.deleteOnMatch = GetObjectValue(config, 'deleteOnMatch', false); + + var _this = this; + + var onKeyDownHandler = function (event) + { + if (_this.matched || !_this.enabled) + { + return; + } + + var matched = ProcessKeyCombo(event.data, _this); + + if (matched) + { + _this.manager.events.dispatch(new KeyComboMatchEvent(_this, event)); + + if (_this.resetOnMatch) + { + ResetKeyCombo(_this); + } + else if (_this.deleteOnMatch) + { + _this.destroy(); + } + } + }; + + this.onKeyDown = onKeyDownHandler; + + this.manager.events.on('KEY_DOWN_EVENT', onKeyDownHandler); }; KeyCombo.prototype.constructor = KeyCombo; KeyCombo.prototype = { + destroy: function () + { + this.enabled = false; + this.keyCodes = []; + this.manager.events.off('KEY_DOWN', this.onKeyDown); + this.manager = undefined; + } }; diff --git a/v3/src/input/keyboard/combo/KeyComboMatchEvent.js b/v3/src/input/keyboard/combo/KeyComboMatchEvent.js new file mode 100644 index 000000000..4f5a02142 --- /dev/null +++ b/v3/src/input/keyboard/combo/KeyComboMatchEvent.js @@ -0,0 +1,15 @@ +var Event = require('../../../events/Event'); + +var KeyComboMatchEvent = function (keyCombo, keyboardEvent) +{ + Event.call(this, 'KEY_COMBO_MATCH_EVENT'); + + this.target = keyCombo; + + this.data = keyboardEvent; +}; + +KeyComboMatchEvent.prototype = Object.create(Event.prototype); +KeyComboMatchEvent.prototype.constructor = KeyComboMatchEvent; + +module.exports = KeyComboMatchEvent; diff --git a/v3/src/input/keyboard/combo/ResetKeyCombo.js b/v3/src/input/keyboard/combo/ResetKeyCombo.js new file mode 100644 index 000000000..8b0f5a14f --- /dev/null +++ b/v3/src/input/keyboard/combo/ResetKeyCombo.js @@ -0,0 +1,12 @@ +var ResetKeyCombo = function (combo) +{ + combo.current = combo.keyCodes[0]; + combo.index = 0; + combo.timeLastMatched = 0; + combo.matched = false; + combo.timeMatched = 0; + + return combo; +}; + +module.exports = ResetKeyCombo;