1

So I have a website and I am looking for a way to redirect a user to another webpage when they press the Esc key.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Dove Man
  • 147
  • 1
  • 4
  • 13

2 Answers2

6

You can use the keydown event and capture the event.key, and if event.key === "Escape", you issue a redirect. Like so:

document.body.addEventListener("keydown", function (event) {
    if (event.key === "Escape") {
        window.location.replace("/*your url here*/");
    }
});
RobG
  • 142,382
  • 31
  • 172
  • 209
mhodges
  • 10,938
  • 2
  • 28
  • 46
1

Use keycode 27 for ESC keys. See the below answer for more details. How to detect escape key press with JavaScript or jQuery?

Community
  • 1
  • 1
Adrianopolis
  • 1,272
  • 1
  • 11
  • 15