Languages
[Edit]
EN

JavaScript - detect when down arrow key was pressed

0 points
Created by:
Lani-Skinner
748

In this article, we would like to show you how to detect when the down 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 (40) of the down arrow key instead of a string (ArrowDown).

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 down 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 down 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

Alternative titles

  1. JavaScript - detect when bottom arrow key was pressed
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