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.