With Fortnite giving users a mini-game if they enter the Konami code while seeing the black hole, it seems like a good time to show this snippet that shows how to detect this code using JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>Konami Code!</title>
<style>
body {
background-color: #C6D8D3;
padding: 30px;
}
h1, p, a {
font-family: sans-serif;
}
h1 {
font-size: 76px;
color: #5B6361;
}
p {
font-size: 24px;
color: #353A39;
cursor: pointer;
margin-bottom: 3em;
}
a {
padding: 10px;
font-size: 24px;
background-color: #5B6361;
color: #FFF;
text-decoration: none;
}
a:hover {
background-color: #353A39;
}
</style>
</head>
<body>
<div id="container">
<h1>Konami Code</h1>
<p>Click here to give the page focus and enter the Konami code!</p>
<a href="https://forum.kirupa.com">Learn More</a>
</div>
<script>
let keys = {
37: "left",
38: "up",
39: "right",
40: "down",
65: "a",
66: "b"
};
let konamiCode = ["up", "up", "down", "down", "left", "right", "left", "right", "b", "a"];
let keyCount = 0;
let timerID;
document.addEventListener("keydown", checkCode, false);
function checkCode(e) {
var keyPressed = keys[e.keyCode];
if (keyPressed === konamiCode[keyCount]) {
keyCount++;
// clear timer
window.clearTimeout(timerID);
// start timer with a 1 second delay before resetting state
timerID = window.setTimeout(resetKeyState, 1000);
// check if we are still on the right path
if (keyCount === konamiCode.length) {
cheatCodeActivated();
resetKeyState();
}
} else {
resetKeyState();
}
}
function cheatCodeActivated() {
alert("Cheat code activated!");
}
function resetKeyState() {
console.log("Resetting state!");
window.clearTimeout(timerID);
keyCount = 0;
}
</script>
</body>
</html>
I’ll write a tutorial eventually on it, but it uses techniques showed in the Working With the Keyboard and Timers tutorials.
Cheers,
Kirupa