Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 883 Bytes

prefer-keyboard-event-key.md

File metadata and controls

39 lines (29 loc) · 883 Bytes

Prefer KeyboardEvent#key over KeyboardEvent#keyCode

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.

Fail

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

Pass

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