-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKonamiCode.js
46 lines (38 loc) · 1.02 KB
/
KonamiCode.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
var Konami = (function() {
//privates
var KONAMIATTEMPT = "";
var THECODE = "38384040373937396665";
var konamiCode, bindEvents;
konamiCode = function(keycode) {
//Add this key press to variable to track presses
KONAMIATTEMPT += keycode;
//If the attempt grows more than 10 key presses, remove the first in the list
if(KONAMIATTEMPT.length === 22)
{
KONAMIATTEMPT = KONAMIATTEMPT.slice(2);
}
//See if attempt matches the Konami Code
if (THECODE === KONAMIATTEMPT)
{
//Do your whatever you'd like to do here once the user has successfully entered the Konami Code
alert("The konami code hit!");
}
};
bindEvents = function() {
function keyDown(e) {
//Save keycode of button press as string
var keycode = "" + (e.keyCode ? e.keyCode : e.which);
//Call konamiCode function to check
konamiCode(keycode);
}
document.addEventListener("keydown", keyDown, false);
};
//obj to set to Konami module
var obj = {
init: function() {
bindEvents();
}
};
return obj;
})();
Konami.init();