Skip to content

Commit 96a44ee

Browse files
authored
Improved konami code (#1640)
* Update README.md * Create KonamiCodeEasterEggV2.js * Added image
1 parent 12c8338 commit 96a44ee

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function(spModal) {
2+
var c = this;
3+
console.log('Lol what are you doing here?');
4+
5+
const KONAMI_CODE = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown',
6+
'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'];
7+
let inputSequence = [];
8+
let timeoutId;
9+
10+
const handleKeyPress = (e) => {
11+
// Clear timeout to reset sequence if user pauses too long
12+
clearTimeout(timeoutId);
13+
14+
// Add key to sequence
15+
inputSequence.push(e.key);
16+
17+
// Keep only the last N keys (length of Konami code)
18+
if (inputSequence.length > KONAMI_CODE.length) {
19+
inputSequence.shift();
20+
}
21+
22+
// Check if current sequence matches Konami code
23+
if (inputSequence.join(',') === KONAMI_CODE.join(',')) {
24+
activateCheats();
25+
inputSequence = []; // Reset after activation
26+
}
27+
28+
// Reset sequence after 2 seconds of inactivity
29+
timeoutId = setTimeout(() => {
30+
inputSequence = [];
31+
}, 2000);
32+
};
33+
34+
const activateCheats = () => {
35+
spModal.open({
36+
size: 'sm',
37+
title: 'Cheats activated',
38+
message: 'Konami code entered',
39+
buttons: [{ label: '${Close}', cancel: true }]
40+
});
41+
};
42+
43+
document.addEventListener('keydown', handleKeyPress);
44+
45+
// Cleanup listener when widget is destroyed
46+
c.$onDestroy = function() {
47+
document.removeEventListener('keydown', handleKeyPress);
48+
clearTimeout(timeoutId);
49+
};
50+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
# Konami Code Easter Egg
22

33
Put this code in the client controller of a widget to listen for the Konami Code. By default it just opens a modal notifying the user that the konami code as activated. Modify to do whatever fun things you want.
4+
5+
## Version 2
6+
7+
[KonamiCodeEasterEggV2.js]("Modern Development\Service Portal Widgets\Konami Code Easter Egg\KonamiCodeEasterEggV2.js") is the same code but improved with:
8+
9+
1. Uses e.key instead of e.keyCode (which is deprecated) with modern arrow key names
10+
2. Automatically tracks only the last N keypresses instead of manual position tracking
11+
3. Resets the sequence if the user pauses too long (more forgiving UX)
12+
4. Removes event listener when widget is destroyed to prevent memory leaks
13+
5. Uses array join comparison instead of position tracking
14+
6. Modern variable declarations for better scoping
15+
16+
<img width="314" height="205" alt="image" src="https://github.com/user-attachments/assets/ea39dbdf-c252-4f7f-942d-8f26319ca6e2" />

0 commit comments

Comments
 (0)