I'm currently working on a personal website as a fun project, and I had the idea of adding an easter egg feature where if a user presses the letter "e" anywhere on the site, a small image of a speech balloon will briefly appear and then disappear. Ideally, it would also make a sound or randomly appear on different parts of the page, but my main goal right now is just to get it functioning properly. Unfortunately, I don't have much experience with JavaScript.
I've been doing some research and found examples of displaying an image when clicked, as well as translating key presses into click events, but I'm having trouble putting everything together correctly. I've tried several methods but haven't had any success so far. I've even looked into scripts that create images when clicked, but I'm not sure how to adapt that to a key press event.
Here is what I have managed to come up with so far. From my understanding, I've set up an event listener to detect when the "E" key is pressed, and then it should call the showImage function to change the display property of the div from 'none' to 'block'. However, when I press the "E" key, nothing happens.
<script>
var addEvent = document.addEventListener ? function(target,type,action){
if(target){
target.addEventListener(type,action,false);
}
} : function(target,type,action){
if(target){
target.attachEvent('on' + type,action,false);
}
}
addEvent(document,'keydown',function(e){
e = e || window.event;
var key = e.which || e.keyCode;
if(key===101){
showImage();
}
});
const showImage = () => {
document.getElementById("first").style.display ='block';
}
</script>
<body>
<div id="first" style="height:400px; width:400px; display:none;">
<img src="test.png">
</div>
</body>