A simple library for creating custom keyboard shortcuts (or "easter eggs") for your web pages.
First, include the script in your HTML File.
<script src="https://cdn.jsdelivr.net/gh/wuemeli/egg.js/egg.min.js"></script>
Then, create a new instance of the EasterEgg class:
const easterEgg = new EasterEgg();
You can now add easter eggs using the addEgg method. The method takes two arguments: an array of key codes representing the key sequence, and a callback function to be executed when the key sequence is entered:
easterEgg.addEgg([38, 38, 40, 40, 37, 39, 37, 39, 66, 65], () => {
console.log("Konami code entered!");
});
You can add as many eggs as you want and customize it as per your requirement.
Creates a new instance of the Easter Egg library.
Adds a new easter egg to the library. keys is an array of key codes, representing the key sequence that should trigger the easter egg. fn is the callback function that will be executed when the key sequence is entered.
Removes the egg with the specified key sequence
Removes all eggs
Checks if a specific egg exists with the key sequence
Executes the egg with the specified key sequence
Handles the keydown event, it checks if the key pressed is not in ignored keys and if the entered keys match any egg key sequence.
An array of key codes that will be ignored by the library. By default, the shift key (code 16) is ignored.
const easterEgg = new EasterEgg(); easterEgg.addEgg([38, 38, 40, 40, 37, 39, 37, 39, 66, 65], () => { console.log("Konami code!"); });
This example creates a new instance of the EasterEgg class, and then adds an egg that listens for the Konami code (up, up, down, down, left, right, left, right, B, A) and when the user presses that sequence of keys, the function passed to the addEgg method will be invoked.
This library only handle keydown event, so you will not be able to use it for sequence of keys that involve keyup or keypress events.