Skip to content

unicorn/prefer-keyboard-event-key Style

🛠️ An auto-fix is available for this rule.

What it does

Enforces the use of KeyboardEvent#key over KeyboardEvent#keyCode which is deprecated. The .key property is also more semantic and readable.

Why is this bad?

The keyCode, which, and charCode properties are deprecated and should be avoided in favor of the key property.

Examples

Examples of incorrect code for this rule:

js
window.addEventListener('keydown', event => {
	if (event.keyCode === 8) {
		console.log('Backspace was pressed');
	}
});

window.addEventListener('keydown', event => {
	console.log(event.keyCode);
});

Examples of correct code for this rule:

js
window.addEventListener('keydown', event => {
    if (event.key === 'Backspace') {
    	console.log('Backspace was pressed');
    }
});

window.addEventListener('click', event => {
	console.log(event.key);
});

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
    "rules": {
        "unicorn/prefer-keyboard-event-key": "error"
    }
}
bash
oxlint --deny unicorn/prefer-keyboard-event-key

References

Released under the MIT License.