Skip to content
← Back to rules

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"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-keyboard-event-key": "error",
  },
});
bash
oxlint --deny unicorn/prefer-keyboard-event-key

Version ​

This rule was added in v1.33.0.

References ​