ARTLUNG LAB Share

November 2011

Listen for Key Presses on a Web Page

This sample uses the .which, property, which is deprecated. See https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/which

Keycode:

Source


function checkKeyCode(e) {
    if (e) {
        return e.which;
    }
    return ' ';
}

document.addEventListener('DOMContentLoaded', function () {
    document.addEventListener('keydown', function (evt) {
        document.querySelector('h1 span#keycode').textContent = checkKeyCode(evt);
    });
});