Dove Man Dove Man - 1 month ago 7
HTML Question

Redirect on Key Press?

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.

Answer

You can use the keydown event and capture the event.keyCode, and if it is === 27 (esc), you issue a redirect. Like so:

document.body.addEventListener("keydown", function (event) {
    if (event.keyCode === 27) {
        window.location.replace("/*your url here*/");
    }
});