-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect-konami-code.js
31 lines (28 loc) · 976 Bytes
/
detect-konami-code.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
// reference: https://www.sitepoint.com/jquery-konami-code-listener
// https://stackoverflow.com/questions/35394937/keyboardevent-keycode-deprecated-what-does-this-mean-in-practice
function onKonamiCode() {
alert("Konami code detected!");
}
(function detectKonamiCode() {
if (window.addEventListener) {
var seq = [];
var konamiAsciiSequence = "38,38,40,40,37,39,37,39,66,65";
var sequenceInWords =
"ArrowUp,ArrowUp,ArrowDown,ArrowDown,ArrowLeft,ArrowRight,ArrowLeft,ArrowRight,b,a";
window.addEventListener(
"keydown",
function (event) {
var key = event.key || event.which || event.keyCode;
seq.push(key);
var hitKonamiSequence =
seq.toString().indexOf(konamiAsciiSequence) >= 0;
var hitWordSequence = seq.toString().indexOf(sequenceInWords) >= 0;
if (hitKonamiSequence || hitWordSequence) {
onKonamiCode();
seq = [];
}
},
true
);
}
})();