键盘控制应用范围非常广泛,比如快捷键控制页面的滚动;在填写表单时候,限制输入内容;或者是屏蔽复制、粘贴、退后等功能。这里说说用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) |
BackSpace | 8 | Tab | 9 |
Clear | 12 | Enter | 13 |
Shift_L | 16 | Control_L | 17 |
Alt_L | 18 | Pause | 19 |
Caps_Lock | 20 | Escape | 27 |
Space | 32 | Prior | 33 |
Next | 34 | End | 35 |
Home | 36 | Left | 37 |
Up | 38 | Right | 39 |
Down | 40 | Select | 41 |
42 | Execute | 43 | |
Insert | 45 | Delete | 46 |
Help | 47 | 0 equal braceright | 48 |
1 exclam onesuperior | 49 | 2 quotedbl twosuperior | 50 |
3 section threesuperior | 51 | 4 dollar | 52 |
5 percent | 53 | 6 ampersand | 54 |
7 slash braceleft | 55 | 8 parenleft bracketleft | 56 |
9 parenright bracketright | 57 | a A | 65 |
b B | 66 | c C | 67 |
d D | 68 | e E EuroSign | 69 |
f F | 70 | g G | 71 |
h H | 72 | i I | 73 |
j J | 74 | k K | 75 |
l L | 76 | m M mu | 77 |
n N | 78 | o O | 79 |
p P | 80 | q Q at | 81 |
r R | 82 | s S | 83 |
t T | 84 | u U | 85 |
v V | 86 | w W | 87 |
x X | 88 | y Y | 89 |
z Z | 90 | KP_0 | 96 |
KP_1 | 97 | KP_2 | 98 |
KP_3 | 99 | KP_4 | 100 |
KP_5 | 101 | KP_6 | 102 |
KP_7 | 103 | KP_8 | 104 |
KP_9 | 105 | KP_Multiply | 106 |
KP_Add | 107 | KP_Separator | 108 |
KP_Subtract | 109 | KP_Decimal | 110 |
KP_Divide | 111 | F1 | 112 |
F2 | 113 | F3 | 114 |
F4 | 115 | F5 | 116 |
F6 | 117 | F8 | 119 |
F9 | 120 | F10 | 121 |
F11 | 122 | F12 | 123 |
F13 | 124 | F14 | 125 |
F15 | 126 | F16 | 127 |
F17 | 128 | F18 | 129 |
F19 | 130 | F20 | 131 |
F21 | 132 | F22 | 133 |
F23 | 134 | F24 | 135 |
Num_Lock | 136 | Scroll_Lock | 137 |
Acute grave | 187 | Comma semicolon | 188 |
Minus underscore | 189 | Period colon | 190 |
Numbersign apostrophe | 192 | Plusminus hyphen macron | 210 |
Copyright registered | 212 | Guillemotleft guillemotright | 213 |
Masculine ordfeminine | 214 | ae AE | 215 |
Cent yen | 216 | Questiondown exclamdown | 217 |
Onequarter onehalf threequarters | 218 | Less greater bar | 219 |
Plus asterisk asciitilde | 221 | Multiply division | 227 |
acircumflex Acircumflex | 228 | ecircumflex Ecircumflex | 229 |
icircumflex Icircumflex | 230 | ocircumflex Ocircumflex | 231 |
ucircumflex Ucircumflex | 232 | ntilde Ntilde | 233 |
yacute Yacute | 234 | oslash Ooblique | 235 |
aring Aring | 236 | ccedilla Ccedilla | 237 |
thorn THORN | 238 | eth ETH | 239 |
diaeresis cedilla currency | 240 | agrave Agrave atilde Atilde | 241 |
egrave Egrave | 242 | igrave Igrave | 243 |
ograve Ograve otilde Otilde | 244 | ugrave Ugrave | 245 |
adiaeresis Adiaeresis | 246 | ediaeresis Ediaeresis | 247 |
idiaeresis Idiaeresis | 248 | odiaeresis Odiaeresis | 249 |
udiaeresis Udiaeresis | 250 | ssharp question backslash | 251 |
asciicircum degree | 252 | 3 sterling | 253 |
Mode_switch | 254 | ||
A | 0X65 | B | 0X66 |
C | 0X67 | D | 0X68 |
E | 0X69 | F | 0X70 |
J | 0X71 | H | 0X72 |
I | 0X73 | J | 0X74 |
K | 0X75 | L | 0X76 |
M | 0X77 | N | 0X78 |
O | 0X79 | P | 0X80 |
Q | 0X81 | R | 0X82 |
S | 0X83 | T | 0X84 |
U | 0X85 | V | 0X86 |
W | 0X87 | X | 0X88 |
Y | 0X89 | Z | 0X90 |
0 | 0X48 | 1 | 0X49 |
2 | 0X50 | 3 | 0X51 |
4 | 0X52 | 5 | 0X53 |
6 | 0X54 | 7 | 0X55 |
8 | 0X56 | 9 | 0X57 |
ESC | 0X1B | CTRL | 0X11 |
SHIFT | 0X10 | ENTER | 0XD |