Skip to content

Commit

Permalink
fixed RobertWHurst#31 Multi combos work again.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertWHurst committed Nov 23, 2012
1 parent fda8155 commit ee885e8
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
})(this, function(env) {
var KeyboardJS = {}, locales, locale, map, macros, activeKeys = [], bindings = [], activeBindings = [], activeMacros = [];

//INDEXOF POLLYFILL
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});

//BUNDLED LOCALES
locales = {
'us': {
"map": {
Expand Down Expand Up @@ -322,56 +324,78 @@
}
}
function createBinding(keyCombo, keyDownCallback, keyUpCallback) {
var binding = {}, bindingApi = {};
if(typeof keyCombo === 'object' && typeof keyCombo.push === 'function') {
keyCombo = stringifyKeyCombo(keyCombo);
var binding, subBindings = [], bindingApi = {}, kI, subCombo;
if(typeof keyCombo === 'string') {
keyCombo = parseKeyCombo(keyCombo);
}
if(typeof keyCombo === 'function') {
keyCombo = false;
keyUpCallback = keyDownCallback;
keyDownCallback = keyCombo;
console.log('BIND', keyCombo);
for(kI = 0; kI < keyCombo.length; kI += 1) {
binding = {};

subCombo = stringifyKeyCombo([keyCombo[kI]]);

if(typeof subCombo === 'function') {
subCombo = false;
keyUpCallback = keyDownCallback;
keyDownCallback = subCombo;
}

if(subCombo !== false && typeof subCombo !== 'string') { throw new Error('Failed to bind key combo. The key combo must be string.'); }
binding.keyCombo = subCombo;
binding.keyDownCallback = [];
binding.keyUpCallback = [];
if(keyDownCallback) { binding.keyDownCallback.push(keyDownCallback); }
if(keyUpCallback) { binding.keyUpCallback.push(keyUpCallback); }
bindings.push(binding);
subBindings.push(binding);
}
if(keyCombo !== false && typeof keyCombo !== 'string') { throw new Error('Failed to bind key combo. The key combo must be string.'); }
binding.keyCombo = keyCombo;
binding.keyDownCallback = [];
binding.keyUpCallback = [];
if(keyDownCallback) { binding.keyDownCallback.push(keyDownCallback); }
if(keyUpCallback) { binding.keyUpCallback.push(keyUpCallback); }
bindings.push(binding);

return {
"clear": clear,
"on": on
};

function clear() {
bindings.splice(bindings.indexOf(binding), 1);
var bI;
for(bI = 0; bI < subBindings.length; bI += 1) {
bindings.splice(bindings.indexOf(subBindings[bI]), 1);
}
}

function on(eventName ) {
var callbacks, cI;
console.log(eventName);
var callbacks, cI, bI;

if(typeof eventName !== 'string') { throw new Error('Cannot bind callback. The event name must be a string.'); }
if(eventName !== 'keyup' && eventName !== 'keydown') { throw new Error('Cannot bind callback. The event name must be a "keyup" or "keydown".'); }
callbacks = Array.prototype.slice.apply(arguments, [1]);
for(cI = 0; cI < callbacks.length; cI += 1) {
if(typeof callbacks[cI] === 'function') {
if(eventName === 'keyup') {
binding.keyUpCallback.push(callbacks[cI]);
for(bI = 0; bI < subBindings.length; bI += 1) {
subBindings[bI].keyUpCallback.push(callbacks[cI]);
}
} else {
binding.keyDownCallback.push(callbacks[cI]);
for(bI = 0; bI < subBindings.length; bI += 1) {
subBindings[bI].keyDownCallback.push(callbacks[cI]);
}
}
}
}

return { "clear": clear };

function clear() {
var cI;
var cI, bI;
for(cI = 0; cI < callbacks.length; cI += 1) {
if(typeof callbacks[cI] === 'function') {
if(eventName === 'keyup') {
binding.keyUpCallback.splice(binding.keyUpCallback.indexOf(callbacks[cI]), 1);
for(bI = 0; bI < subBindings.length; bI += 1) {
subBindings[bI].keyUpCallback.splice(subBindings[bI].keyUpCallback.indexOf(callbacks[cI]), 1);
}
} else {
binding.keyDownCallback.splice(binding.keyDownCallback.indexOf(callbacks[cI]), 1);
for(bI = 0; bI < subBindings.length; bI += 1) {
subBindings[bI].keyDownCallback.splice(subBindings[bI].keyDownCallback.indexOf(callbacks[cI]), 1);
}
}
}
}
Expand Down Expand Up @@ -655,7 +679,7 @@
* @param {String} keyName The key name string.
*/
function removeActiveKey(keyName) {
if(keyName === 'super') { activeKeys = []; }
if(keyName === 'super') { activeKeys = []; } //remove all key on release of super.
activeKeys.splice(activeKeys.indexOf(keyName), 1);
}

Expand Down

0 comments on commit ee885e8

Please sign in to comment.