Three event differences between KeyDown, KeyUp, and KeyPress in JavaScript

Source: Internet
Author: User
Tags numeric

In JS three events are key events, let's take a look at the keydown,keyup,keypress these three events in the difference where it is, I hope the example can help you.

1. Keys to raise events

Non-character keys do not raise KeyPress events, but non-word keys can raise KeyDown and KeyUp events. The KeyPress event Keychar property can get the characters entered.

2. Time caused by the event

The KeyDown and KeyPress events occur when the key is pressed, and the KeyUp event occurs when the key is released.

3. Sequence of occurrences

KeyDown-> KeyPress-> KeyUp. If you press a key for a long time to loosen up, the event that occurs is: KeyDown-> KeyPress-> KeyDown-> KeyPress-> KeyDown,-> KeyPress-> ...-> KeyUp.

Attention:

KeyDown triggered, not necessarily trigger KeyUp, when KeyDown pressed, drag the mouse, then will not trigger the KeyUp event.

KeyPress is used primarily to capture numbers (note: Symbols that include shift+ numbers), letters (note: include case), keypad, and so on except f1-12, SHIFT, Alt, Ctrl, Insert, Home, PgUp, Delete, end, PgDn, ANSI characters ScrollLock, Pause, NumLock, {Menu key}, {Start key}, and directional keys

KeyDown and KeyUp can usually capture the keyboard except PRSCRN all keys (special keys for special keyboards are not discussed here)

KeyPress can only capture a single character

KeyDown and KeyUp can capture key combinations.

KeyPress can capture the case of a single character

KeyDown and KeyUp for a single character capture keyvalue are a value, that is, can not determine the case of a single character.

KeyPress does not distinguish between numeric characters of the keypad and the main keyboard.

KeyDown and KeyUp distinguish the numeric characters of the keypad and the main keyboard.

None of the Prscrn buttons KeyPress, KeyDown, and KeyUp can be captured.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.