Finished off Key Combo support. All config options now working correctly.

This commit is contained in:
Richard Davey 2017-02-21 23:01:05 +00:00
parent 101be294e0
commit fa69035e4f
6 changed files with 76 additions and 7 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '6393ce50-f872-11e6-ba94-79064404f409'
build: '9d366fd0-f888-11e6-8597-e13c3c71fc73'
};
module.exports = CHECKSUM;

View file

@ -2,6 +2,7 @@ var Event = function (type)
{
this.type = type;
// The element that initiated the event.
this.target;
this._propagate = true;

View file

@ -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

View file

@ -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;
}
};

View file

@ -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;

View file

@ -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;