Differences between KeyDown, KeyUp, and KeyPress events in Javascript

Source: Internet
Author: User

Differences between KeyDown, KeyUp, and KeyPress events in Javascript

The three events in js are key events. Let's take a look at the differences among the three events: KeyDown, KeyUp, and KeyPress. I hope the example can help you.

1. buttons that cause the event

Non-character keys do not trigger KeyPress events, but non-character keys can trigger KeyDown and KeyUp events. The KeyChar attribute of the KeyPress event can be used to obtain the input characters.

2. Event-triggered time

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

3. event occurrence Sequence

KeyDown-> KeyPress-> KeyUp. If you press a key for a long time, the event is: KeyDown-> KeyPress->...-> KeyUp.

Note:

KeyUp is not necessarily triggered after KeyDown is triggered. When KeyDown is pressed, drag the mouse to trigger the KeyUp event.

KeyPress is mainly used to capture numbers (Note: including Shift + digit symbols) and letters (Note: including uppercase and lowercase), keypad, in addition to F1-12, SHIFT, Alt, Ctrl, Insert, Home, PgUp, Delete, End, PgDn, ScrollLock, Pause, NumLock, {menu key}, {START key} and ANSI characters outside the direction key

KeyDown and KeyUp can usually capture all the buttons on the keyboard except PrScrn (the special keys on the special keyboard are not discussed here)

KeyPress can only capture a single character

KeyDown and KeyUp can capture the combination of keys.

KeyPress can capture the case sensitivity of a single character

KeyDown and KeyUp are values for the KeyValue captured by a single character, that is, the case of a single character cannot be determined.

KeyPress does not distinguish between a keypad and a keyboard's numeric characters.

KeyDown and KeyUp distinguish between numbers on the keypad and the primary keypad.

The PrScrn buttons KeyPress, KeyDown, and KeyUp cannot 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.