prefer-keyboard-event-key
March 27, 2026 ยท View on GitHub
๐ Prefer KeyboardEvent#key over KeyboardEvent#keyCode.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ง This rule is automatically fixable by the --fix CLI option.
Enforces the use of KeyboardEvent#key over KeyboardEvent#keyCode which is deprecated. The .key property is also more semantic and readable.
This rule is partly fixable. It can only fix direct property access.
Examples
// โ
window.addEventListener('keydown', event => {
if (event.keyCode === 8) {
console.log('Backspace was pressed');
}
});
// โ
window.addEventListener('keydown', event => {
if (event.key === 'Backspace') {
console.log('Backspace was pressed');
}
});
// โ
window.addEventListener('keydown', event => {
console.log(event.keyCode);
});
// โ
window.addEventListener('click', event => {
console.log(event.key);
});