-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.konamicode.1.3.js
executable file
·72 lines (57 loc) · 2.16 KB
/
jquery.konamicode.1.3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Konami Code v1.3 - implements the feature of the Konami Code into jQuery
* written by Dominik Kukacka
* http://codecookie.net/blog/konamicode
*
* Copyright (c) 2010 Dominik Kukacka (http://codecookie.net/)
* Dual licensed under the MIT (http://bit.ly/TTtsU)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Built for jQuery library
* http://jquery.com
*
*/
var UP = 38, DOWN = 40, LEFT = 37, RIGHT = 39, A = 65, B = 66;
(function($){
$.fn.konamicode = function(options) {
var defaults = {
speed: 3500,
correctCombination: [UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A],
onCorrectCombination : function(){}
};
var options = $.extend(defaults, options);
//the keys which the user have been pressed
var insertedCombination = [];
//our timer
var timer = null;
var timer_started = false;
return this.bind('keyup', function(event) {
//if one of the "command" keys is pressed
if (event.ctrlKey || event.shiftKey || event.altKey) return;
// if the key which was pressed is correct
if (event.keyCode == options.correctCombination[insertedCombination.length]) {
//if timer is not started start it
if( !timer_started ) {
timer = setTimeout(function(){
reset();
}, options.speed);
timer_started = true;
}
//last is correct --> reset annd call callback
if (insertedCombination.length == options.correctCombination.length - 1) {
reset();
options.onCorrectCombination.call(this);
} else { // not the last in kombo --> insert
insertedCombination[insertedCombination.length] = event.keyCode;
}
} else {
reset(); // wrong key dude
}
});
function reset() {
timer_started = false;
clearTimeout(timer);
insertedCombination = [];
}
};
})(jQuery);