My Eventlistener for "Escape" doesn't work, but if I put in "Enter" instead of "Escape" it works with "Enter".
Here is my Code:
addEventListener("keypress", function (event) {
if (event.key === "Escape") {
spanClick();
}
});
The issue is that the "Escape" key is not detected by the keypress
event. The keypress
event is generally used for character keys only and not special keys. Use the keydown
or keyup
event instead.