JSFiddle - React, Tailwind, and code Playground
JavaScript
var eventCombo = {
comboTree: {}
};
eventCombo.add = function (fn) {
var node = this.comboTree;
var args = [].slice.call(arguments, 1);
args.forEach(function (keyCode) {
if (node[keyCode] === undefined) {
node[keyCode] = {};
}
node = node[keyCode];
}, this);
node.fn = fn;
};
eventCombo.handleKey = function (key) {
if (this._stage === undefined) this._stage = this.comboTree;
if (this._stage[key]) {
this._stage = this._stage[key];
if (this._stage.fn) {
console.log('Found a function');
this._stage.fn();
this.resetStage();
}
}
};
eventCombo.resetStage = function () {
this._stage = this.comboTree;
};
eventCombo.add(function () {
console.log('Ctrl + S called');
}, 17, 83);
$(document).keydown(function (e) {
eventCombo.handleKey(e.keyCode);
e.preventDefault();
e.stopPropagation();
});