Skip to content

Latest commit

Β 

History

History
46 lines (35 loc) Β· 1.33 KB

File metadata and controls

46 lines (35 loc) Β· 1.33 KB

prefer-keyboard-event-key

πŸ“ 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);
});