Home  >  Q&A  >  body text

Escape Eventlistener doesn't work, but when I type Enter it does?

My "Escape" event listener doesn't work, but if I type "Enter" instead of "Escape" it works with "Enter".

This is my code:

addEventListener("keypress", function (event) {
  if (event.key === "Escape") {
    spanClick();
  }
});

P粉362071992P粉362071992213 days ago287

reply all(1)I'll reply

  • P粉523625080

    P粉5236250802024-03-21 09:02:12

    The problem is that the keypress event does not detect the "Escape" key. keypress events are generally only used for character keys and not special keys. Please use the keydown or keyup event instead.

    reply
    0
  • Cancelreply