JS KeyUp, KeyPress and KeyDown events

Source: Internet
Author: User

Reproduced from: http://www.cnblogs.com/manongxiaobing/archive/2012/11/05/2755412.html


JS KeyUp, KeyPress, and KeyDown events are all about keyboard events

When a button is pressed or released in every modern browser, there can be three kinds of client events. KeyDown event KeyPress Event KeyUp Event

The KeyDown event occurs when the key of the keyboard is pressed, and then the KeyPress event is triggered. The KeyUp event is triggered when the key is released.

Examples of how these three events are used in the page are as follows:

<input id= "testkeyevent" name= "testkeyevent" onKeyUp = "KeyUp ()"/>

<input id= "testkeyevent" name= "testkeyevent" onkeypress = "keypress ()"/>

<input id= "testkeyevent" name= "testkeyevent" onkeydown = "KeyDown ()"/>

The corresponding JS function:

function KeyUp () {...}

function KeyPress () {...}

function KeyDown () {...}

Note: After the KeyDown trigger, the KeyUp is not necessarily triggered, and when KeyDown is pressed, drag the mouse, then the KeyUp event will not be triggered. KeyPress is primarily used 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, The ANSI characters KeyDown and KeyUp outside the Delete, end, PgDn, ScrollLock, Pause, NumLock, {Menu key}, {Start key}, and directional keys can usually capture the keyboard except PRSCRN all keys ( This does not discuss special keys for special keyboards) KeyPress can only capture single characters KeyDown and KeyUp to capture key combinations. KeyPress can capture the case of a single character KeyDown and KeyUp the keyvalue captured for a single character is a value, that is, the case of a single character cannot be judged. 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.

When you use the keyboard, you will typically use a key combination function similar to Ctrl+shift+alt. How do we determine that?

Can be handled through the KeyUp event (here's why you don't need to KeyDown, because when you decide KeyDown, CTRL, Shift, and ALT belong to the pressed state, and then another key is not exactly capturing the key combination, so use the KeyDown Can not be accurately judged by the KeyUp event to determine.

Here is a simple list of CTRL + other key combinations to determine the code:
private void Form3_keyup (object sender, KeyEventArgs e)
{
if (E.control)
{
MessageBox.Show ("keyup:ctrl+" + e.keyvalue.tostring ());
}
Capture Prscrn Key Event

A hook can be used to determine the PRSCRN key event, and the hook gets any keyboard events.

Here are the code for some keys on the keyboard: Here are the code for some keys on the keyboard:

keyboard keys corresponding to the digital

Backspace 8
tab 9
Enter 13
Shift 16
Ctrl 17
Alt 18
Pause/break 19
Caps LOCK 20
Escape 27
Page UP 33
MySpace 32
Page Down 34
End 35
Home 36
Arrow Left 37
Arrow up 38
Arrow Right 39
Arrow Down 40
Insert 45
Delete 46
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
Left window key 91
Right window key 92
Select key 93
Numpad 0 96
Numpad 1 97
Numpad 2 98
Numpad 3 99
Numpad 4 100
Numpad 5 101
Numpad 6 102
Numpad 7 103
Numpad 8 104
Numpad 9 105
Multiply 106
Add 107
Subtract 109
decimal point 110
Divide 111
F1 112
F2 113
F3 114
F4 115
F5 116
F6 117
F7 118
F8 119
F9 120
F10 121
F1

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.