More recent and much cleaner: use event.key
. No more arbitrary number codes!
NOTE: The old properties (
.keyCode
and.which
) are Deprecated.
document.addEventListener('keydown', function(event) {
const key = event.key; // const {key} = event; ES6+
if (key === "Delete") {
// Do things
}
});