-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkonamicode.js
56 lines (53 loc) · 1.46 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
47
48
49
50
51
52
53
54
55
56
var KonamiCodeListener =
{
init: function(onsuccess=function(){}, onfail=function(){})
{
window.addEventListener("keydown", this._keyPressEventListener(onsuccess, onfail, this));
return this;
},
destroy: function()
{
window.removeEventListener("keydown", this._keyPressEventListener);
_userInput.length = 0;
return this;
},
_keyPressEventListener: function(onsuccess, onfail, self)
{
return function(e)
{
self._processEntry(e.keyCode, self, onsuccess, onfail);
}
},
_sequence: [ 38, 38, 40, 40, 37, 39, 37, 39, 66, 65 ],
_userInput: [],
_processEntry: function(keyCode, self, onsuccess, onfail)
{
switch (keyCode)
{
case 37:
case 38:
case 39:
case 40:
case 65:
case 66:
self._userInput.push(keyCode);
console.log(keyCode);
break;
default:
self._userInput.length = 0;
return;
}
if(self._sequence.length != self._userInput.length) return;
for ( var i = 0; i < self._userInput.length; i++ )
{
if ( self._userInput[i] != self._sequence[i] )
{
self._userInput.length = 0;
onfail();
return;
}
}
self._userInput.length = 0;
onsuccess();
}
};