首页 > 代码库 > jQuery键盘控制方法,以及键值(keycode)对照表

jQuery键盘控制方法,以及键值(keycode)对照表

键盘控制应用范围非常广泛,比如快捷键控制页面的滚动;在填写表单时候,限制输入内容;或者是屏蔽复制、粘贴、退后等功能。这里说说用jQuery怎么来实现。个人觉得jQuery比原生态的JS好用,代码简单清晰,不要问我JS怎么写,因为我不知道。在现在交互满天飞的年代,你加载一个jQuery大家还是能接受的。

$(window).keydown(function(event){   switch (event.which) {   case(74): //J键   //要执行的操作   break;   case(75)://K键   //要执行的操作   break;   case(72)://H键   //要执行的操作   break;   case(76)://L键   //要执行的操作   break;   }   }); 

如果要使用组合键,则可以利用event.ctrlKey,event.shiftKey,event .altKey判断是否按下了ctrl键、shift键以及alt键。

下面是键值对照表:

键盘名称键值(keycode)键盘名称键值(keycode)
BackSpace8Tab9
Clear12Enter13
Shift_L16Control_L17
Alt_L18Pause19
Caps_Lock20Escape27
Space32Prior33
Next34End35
Home36Left37
Up38Right39
Down40Select41
Print42Execute43
Insert45Delete46
Help470 equal braceright48
1 exclam onesuperior492 quotedbl twosuperior50
3 section threesuperior514 dollar52
5 percent536 ampersand54
7 slash braceleft558 parenleft bracketleft56
9 parenright bracketright57a A65
b B66c C67
d D68e E EuroSign69
f F70g G71
h H72i I73
j J74k K75
l L76m M mu77
n N78o O79
p P80q Q at81
r R82s S83
t T84u U85
v V86w W87
x X88y Y89
z Z90KP_096
KP_197KP_298
KP_399KP_4100
KP_5101KP_6102
KP_7103KP_8104
KP_9105KP_Multiply106
KP_Add107KP_Separator108
KP_Subtract109KP_Decimal110
KP_Divide111F1112
F2113F3114
F4115F5116
F6117F8119
F9120F10121
F11122F12123
F13124F14125
F15126F16127
F17128F18129
F19130F20131
F21132F22133
F23134F24135
Num_Lock136Scroll_Lock137
Acute grave187Comma semicolon188
Minus underscore189Period colon190
Numbersign apostrophe192Plusminus hyphen macron210
Copyright registered212Guillemotleft guillemotright213
Masculine ordfeminine214ae AE215
Cent yen216Questiondown exclamdown217
Onequarter onehalf threequarters218Less greater bar219
Plus asterisk asciitilde221Multiply division227
acircumflex Acircumflex228ecircumflex Ecircumflex229
icircumflex Icircumflex230ocircumflex Ocircumflex231
ucircumflex Ucircumflex232ntilde Ntilde233
yacute Yacute234oslash Ooblique235
aring Aring236ccedilla Ccedilla237
thorn THORN238eth ETH239
diaeresis cedilla currency240agrave Agrave atilde Atilde241
egrave Egrave242igrave Igrave243
ograve Ograve otilde Otilde244ugrave Ugrave245
adiaeresis Adiaeresis246ediaeresis Ediaeresis247
idiaeresis Idiaeresis248odiaeresis Odiaeresis249
udiaeresis Udiaeresis250ssharp question backslash251
asciicircum degree2523 sterling253
Mode_switch254  
A0X65B0X66
C0X67D0X68
E0X69F0X70
J0X71H0X72
I0X73J0X74
K0X75L0X76
M0X77N0X78
O0X79P0X80
Q0X81R0X82
S0X83T0X84
U0X85V0X86
W0X87X0X88
Y0X89Z0X90
00X4810X49
20X5030X51
40X5250X53
60X5470X55
80X5690X57
ESC0X1BCTRL0X11
SHIFT0X10ENTER0XD

jQuery键盘控制方法,以及键值(keycode)对照表