Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

KeyboardEvent.key — the meaning of a key

KeyboardEvent.key is a read-only string property describing a key’s meaning. It accounts for keyboard layout and modifiers; unlike code, it does not describe the key’s physical position.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

event.key

KeyboardEvent.key, read as event.key, returns a string describing a key’s meaning. For printable characters, it accounts for keyboard layout and modifiers, for example "a" or "A". The property is read-only.

UI Events groups named key values into categories: "Enter" is listed under whitespace keys, "Escape" under UI keys, and "ArrowDown" under navigation keys. Space has the value " ", not "Space". "Dead" identifies a dead key used to compose characters, while "Unidentified" means the key could not be identified. String comparisons are case-sensitive.

Use key when the key’s meaning matters, and code when its physical position matters. Do not treat keyboard events as a complete record of text input; for editing, consider beforeinput and input events.

This is a browser Web API defined in UI Events, not a feature of a particular ECMAScript edition. Having Node 24 does not establish support. The example explicitly sets key and code: the constructor does not calculate key from code or shiftKey. It tests value access, not a real keyboard; support in target browsers must be checked separately.

Examples

const events = [
  new KeyboardEvent("keydown", { key: "a", code: "KeyA" }),
  new KeyboardEvent("keydown", { key: "A", code: "KeyA", shiftKey: true }),
  new KeyboardEvent("keydown", { key: "Enter", code: "Enter" })
];

console.log(events.map(event => `${event.key}:${event.code}`).join("\n"));

Result

a:KeyA
A:KeyA
Enter:Enter

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources