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.
Asked
Active
Viewed 4,491 times
2 Answers
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*/");
}
});
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