Methods of keyboard events for JavaScript

Source: Internet
Author: User

Keyboard events include the three events onkeydown, onkeypress, and onkeyup

Event initialization

function KeyDown () {} Document.onkeydown = KeyDown; The KeyDown () function is called when the key on the keyboard is pressed.

Dom Standard

function KeyDown (e) {var keycode = E.which;//get the corresponding key value (number) var Realkey = String.fromCharCode (E.which);//Get the real character that represents the modifier al   ERT ("Key code:" + KeyCode + "character:" + Realkey); } Document.onkeydown = KeyDown

IE under

function KeyDown () {var keycode = Event.keycode; The method of obtaining the key value under//ie var realkey = String.fromCharCode (Event.keycode);   Alert ("Key code:" + KeyCode + "character:" + Realkey); } Document.onkeydown = KeyDown

Compatible approach

function KeyUp (e) {var currkey=0,e=e| |     Event currkey=e.keycode| | e.which| |     E.charcode;     var keyName = String.fromCharCode (Currkey);   Alert ("Key code:" + Currkey + "character:" + keyName); } document.onkeyup = KeyUp;

Usage principle: The KeyDown event is most useful for function keys, while the KeyPress event is most useful for printable keys

The following key values are only fully valid in the text box, if the

tag, only letter keys, numeric keys, and some control keys are available, with the same key value as the ASCII value for the letter and number keys

Key values for letters and number keys (KeyCode)

Key code for Keys

A65

B66

C67

D68

E69

F70

G71

H72

I73

J74

K75

L76

M77

N78

O79

P80

Q81

R82

S83

T84

U85

V86

W87

X88

Y89

Z90

048

149

250

351

452

553

654

755

856

957

Key value (keycode) function key code value (keycode) for keys on numeric keypad

Key code for Keys

096

197

298

399

4100

5101

6102

7103

8104

9105

*106

+107

Enter108

-109

.110

/111

F1112

F2113

F3114

F4115

F5116

F6117

F7118

F8119

F9120

F10121

F11122

F12123

Control key Code value (KeyCode)

Key code for Keys

BackSpace8

Esc27

Right Arrow39

Left Arrow37

Down Arrow40

Up Arrow38

-_189

. >190

Spacebar32

Tab9

Clear12

Page UP33

Page Down34

Enter13

Insert45

;: 186

Delete46

' ~192

/?191

Num Lock144

Control17

Home36

End35

Shift16

[{219

}]221

\|220

=+187

,

' "222

Cape Lock20

Alt18

Methods of keyboard events for JavaScript

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.