Skip to content

Instantly share code, notes, and snippets.

@tommycoppers
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommycoppers/03593c3ceb009a44b602 to your computer and use it in GitHub Desktop.
Save tommycoppers/03593c3ceb009a44b602 to your computer and use it in GitHub Desktop.
;(function keycombo ($, namespace) {
function KeyCombo () {
var eventNamespace, // Unique namespace so that keypressing can be unbound
keysPressed = [], // Dynamically populates with key codes as they are actviely pressed
combos = []; // [[keys], callback]
var $document = $(document);
// Private Functions
////////////////////
function addPressedKey (e) {
/**
* Populates keysPressed array with the event's key code if not already present
* Then call matchKeysPressed.
*/
var keycode = e.keyCode;
if (keysPressed.indexOf(keycode) === -1) {
keysPressed.push(keycode);
}
matchKeysPressed();
}
function removePressedKey (e) {
/**
* Remove event's key code on keyup if key code is present in keysPressed array
*/
var keycode = e.keyCode,
keypressIndex = keysPressed.indexOf(keycode);
if (keypressIndex > -1) {
keysPressed.splice(keypressIndex, 1);
}
}
function matchKeysPressed () {
/**
* If key combinations have been added to the combos array, loop through the combinations.
* Within each combination, check the keysPressed array for the keys needed to be active for the callback to execute.
* If a key isn't found in keysPressed array, turn the doCallback switch to false and do not perform the callback.
* If all the keys exist, do the callback
*/
if (combos.length) {
for (var i in combos) {
var thisCombo = combos[i];
var thisKeys = thisCombo[0];
var thisCallback = thisCombo[1];
var doCallback = true;
if (thisKeys.length) {
for (var i in thisKeys) {
var key = thisKeys[i];
if (keysPressed.indexOf(key) === -1) {
doCallback = false;
break;
}
}
}
if (doCallback) {
thisCallback();
}
}
}
}
function initialize () {
/**
* Create a random eventNamespace ID that will be unique to this instance of key events.
* Unique namespaces allow the document to be unbound from key presses relative to this instance
* Bind events to document with unique namespace
*/
eventNamespace = '.keycombo_' + Math.ceil(Math.random()*100);
$document
.on('keydown' + eventNamespace, function (e) {
addPressedKey(e);
})
.on('keyup' + eventNamespace, function (e) {
removePressedKey(e);
});
}
// Private Functions
////////////////////
function getCombos () {
/**
* As a developer, if you need to retrieve a list of active key combinations, call this function.
* Returns - combos array.
*/
return combos;
}
function addCondition (keys, callback) {
/**
* Populates the combos array with an array containing [keys, callback]
* note: keys needs to be an array.
*/
combos.push([keys, callback]);
}
function unbindKeypress () {
/**
* Unbinds keydown and keyup from document
*/
$document.off('keydown' + eventNamespace).off('keyup' + eventNamespace);
}
// Initialize
/////////////
initialize();
return {
combos : getCombos,
add : addCondition,
unbind : unbindKeypress
};
}
namespace.KeyCombo = KeyCombo;
})(window.jQuery, window.namespace || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment