JS DOM - 04.05 - Keyboard Events

keyboard events The keydown events happens when a key is pressed down, and then keyup – when it’s released.

event.code and event.key

The key property of the event object allows to get the character, while the code property of the event object allows to get the “physical key code”.

For instance, the same key Z can be pressed with or without Shift. That gives us two different characters: lowercase z and uppercase Z. The event.key is exactly the character, and it will be different. z Z But event.code is the same KeyZ

  • Letter keys have codes "Key<letter>""KeyA""KeyB" etc.

  • Digit keys have codes: "Digit<number>""Digit0""Digit1" etc.

  • Special keys are coded by their names: "Enter""Backspace""Tab" etc.

  • code – the “key code” ("KeyA""ArrowLeft" and so on), specific to the physical location of the key on keyboard.

  • key – the character ("A""a" and so on), for non-character keys, such as Esc, usually has the same value as code.