0

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,083
  • 7
  • 48
  • 49
Dove Man
  • 137
  • 1
  • 3
  • 12

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
  • 134,457
  • 30
  • 163
  • 204
mhodges
  • 10,480
  • 2
  • 25
  • 45
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,182
  • 1
  • 10
  • 15