Languages
[Edit]
EN

JavaScript - get keyboard key property value

3 points
Created by:
Cassia
1265

In this article, we would like to show you how to get keyboard key code value using JavaScript.

Quick solution:

Where:

  • e.key returns symbol or key name as string,
  • e.code returns key code as a string (similar to a key name in e.key),
  • e.keyCode returns key code as a number - deprecated approach.

Note: keydown can be changed to keyup up keypress.

 

Practical example

Edit

In this example, we use the keycode and keyCode properties to get information about pressed key.

Warnings:

  • key and code were introduced around 2016 and is recommended to use,
  • keyCode is marked as depricated but is still used as legacy.

 

References

Edit
  1. KeyboardEvent.key - Web APIs | MDN

Alternative titles

  1. JavaScript - display keyboard key code value
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join