Search code examples
javascriptaddeventlistenerkeypressevent-listener

Escape Eventlistener doesn't work but when i put enter in it, it works?


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();
  }
});


Solution

  • 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.