Languages
[Edit]
EN

JavaScript - Keyboard Events

0 points
Created by:
user4838
698

In this article, we would like to show you how to handle keyboard events and the differences between them in JavaScript.

Quick solution:

 

Introduction

Edit

There are three keyboard events in JavaScript that describe user interaction with the keyboard:

  • onkeydown
  • onkeypress
  • onkeyup

1. onkeydown event

Edit

The event occurs when the user is pressing a key on the keyboard.

Practical example

Edit

In this example, we will execute myFunction() on key down event inside the input field.

2. onkeypress event

Edit

The event occurs when the user presses a key (when a character is being typed).

Practical example

Edit

In this example, we will execute myFunction() on key press event inside the input field.

3. onkeyup event

Edit

The event occurs when the user releases a key on the keyboard.

Practical example

Edit

In this example, we will execute myFunction() on key up event inside the input field.

Differences

Edit

The main difference is the order in which the events occur. You can see it in the example below.

The order of keyboard events:

  1. onkeydown
  2. onkeypress
  3. onkeyup

Runnable example:

Alternative titles

  1. JavaScript - onkeydown, onkeypress and onkeyup differences
  2. JavaScript - handle key down, key press and key up
  3. JavaScript - keydown, keypress and keyup differences
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.

JavaScript - events

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