Created
July 26, 2013 16:44
-
-
Save gabteles/6090374 to your computer and use it in GitHub Desktop.
Konami Code jQuery Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Konami Code jQuery Plugin | |
* | |
* Usage: | |
* | |
* 1 - Set permanent callback: everytime the user makes the | |
* konami code the callback will | |
* be called. | |
* | |
* $(document).konamiCode(callback, true) | |
* | |
* | |
* 2 - Non permanent callback: callback will be called only | |
* in the next konami code | |
* | |
* $(document).konamiCode(callback, false) | |
* $(document).konamiCode(callback) | |
* | |
* | |
* 3 - Call konami code without user interation: | |
* | |
* $(document).konamiCode() | |
* | |
* | |
* | |
* Copyright 2013+ Gabriel "Gab!" Teles | |
* Date: 2013/07/26 | |
*/ | |
(function($){ | |
$.konamiCode = { | |
sequence: [38,38,40,40,37,39,37,39,66,65], | |
keys: [], | |
callbacks: [], | |
execute: function(){ | |
$.grep($.konamiCode.callbacks, function(element, index){ | |
(element[0])(); // Callback | |
return element[1]; | |
}); | |
} | |
} | |
$(document).keyup(function(e){ | |
code = e.keyCode; | |
if ($.konamiCode.sequence[$.konamiCode.keys.length] == code){ | |
$.konamiCode.keys.push(code) | |
if ($.konamiCode.keys.length == $.konamiCode.sequence.length){ | |
$.konamiCode.execute() | |
$.konamiCode.keys = [] | |
} | |
} else { | |
$.konamiCode.keys = [] | |
} | |
}); | |
$.fn.konamiCode = function(callback, permanent){ | |
if (typeof(callback) !== 'undefined') | |
$.konamiCode.callbacks.push([callback, !!permanent]); | |
else | |
$.konamiCode.execute(); | |
return this; | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment