Languages
[Edit]
EN

JavaScript - detect when right arrow key was pressed

0 points
Created by:
Aaron1
568

In this article, we would like to show you how to detect when the right arrow key was pressed using JavaScript.

In the below solutions, we use some legacy functions that lets to get key code. The main reason why that approach was used, is the time when event.key and key.code were introduced in the major web browsers (around 2016-2017).

Practical examples

Edit

Both of the examples below provide the legacy support for the keyCode property which represents an integer value (39) of the right arrow key instead of a string (ArrowRight).

Example 1

Edit

In this example, we use event.key to get the value of the key pressed by the user and then check if it's a right arrow.

Example 2

Edit

In this example, we use event.code to get the value of the key pressed by the user and then check if it's a right arrow.

See also

Edit
  1. JavaScript - detect when arrow key was pressed

References

Edit
  1. KeyboardEvent.key - MDN Docs
  2. KeyboardEvent.code - MDN Docs

  3. KeyboardEvent.keyCode - MDN Docs
  4. Document: keydown event - MDN Docs
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