-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-konami-code.js
More file actions
135 lines (115 loc) · 3.35 KB
/
js-konami-code.js
File metadata and controls
135 lines (115 loc) · 3.35 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
(function() {
window.jsKonamiCode = {};
// TODO: You should assign your own callback here.
window.jsKonamiCode.callback = defaultCallback;
window.jsKonamiCode.keyDelayThresholdMs = 700;
window.jsKonamiCode.konamiCodeSequence = [
'ArrowUp',
'ArrowUp',
'ArrowDown',
'ArrowDown',
'ArrowLeft',
'ArrowRight',
'ArrowLeft',
'ArrowRight',
'KeyB',
'KeyA',
];
const KEY_CODE_CONFIGURATIONS = {
'ArrowUp': {
code: 'ArrowUp',
key: 'ArrowUp',
},
'ArrowDown': {
code: 'ArrowDown',
key: 'ArrowDown',
},
'ArrowLeft': {
code: 'ArrowLeft',
key: 'ArrowLeft',
},
'ArrowRight': {
code: 'ArrowRight',
key: 'ArrowRight',
},
'KeyW': {
code: 'KeyW',
key: 'w',
},
'KeyS': {
code: 'KeyS',
key: 's',
},
'KeyA': {
code: 'KeyA',
key: 'a',
},
'KeyD': {
code: 'KeyD',
key: 'd',
},
'KeyB': {
code: 'KeyB',
key: 'b',
},
};
let latestKeyPressTime = -window.jsKonamiCode.keyDelayThresholdMs;
let currentCodeSequence = [];
let currentKeySequence = [];
function setUp() {
document.addEventListener('keydown', onKeyDown, false);
}
function onKeyDown(event) {
// Update the time.
const previousKeyPressTime = latestKeyPressTime;
latestKeyPressTime = Date.now();
// Clear stale key sequences.
if (latestKeyPressTime >= previousKeyPressTime + window.jsKonamiCode.keyDelayThresholdMs) {
currentCodeSequence = [];
currentKeySequence = [];
}
// Record the current key.
currentCodeSequence.push(event.code);
currentKeySequence.push(event.key);
if (checkForMatch()) {
window.jsKonamiCode.callback();
}
}
function checkForMatch() {
const expectedSequence = window.jsKonamiCode.konamiCodeSequence;
if (currentCodeSequence.length < expectedSequence) {
return false;
}
let foundMatch = true;
for (let i = 0; i < expectedSequence.length; i++) {
if (currentCodeSequence[currentCodeSequence.length - 1 - i] !==
expectedSequence[expectedSequence.length - 1 - i]) {
foundMatch = false;
break;
}
}
if (foundMatch) {
return true;
}
foundMatch = true;
for (let i = 0; i < expectedSequence.length; i++) {
if (currentKeySequence[currentKeySequence.length - 1 - i].toLowerCase() !==
KEY_CODE_CONFIGURATIONS[expectedSequence[expectedSequence.length - 1 - i]].key
.toLowerCase()) {
foundMatch = false;
break;
}
}
return foundMatch;
}
function defaultCallback() {
console.log("");
console.log("REST 30");
console.log("");
const audio = document.querySelector('#code-confirmation-sound');
audio.volume = 0.08;
audio.play();
}
window.addEventListener('load', setUp, false);
})();