首页 > 编程 > JavaScript > 正文

js获取及判断键盘按键的方法

2019-11-20 11:06:51
字体:
来源:转载
供稿:网友

本文实例讲述了js获取及判断键盘按键的方法。分享给大家供大家参考,具体如下:

js 里面的键盘事件经常用到,所以收集了键盘事件对应的键码来分享下:

keyCode 8 = BackSpace BackSpacekeyCode 9 = Tab TabkeyCode 12 = ClearkeyCode 13 = EnterkeyCode 16 = Shift_LkeyCode 17 = Control_LkeyCode 18 = Alt_LkeyCode 19 = PausekeyCode 20 = Caps_LockkeyCode 27 = Escape EscapekeyCode 32 = spacekeyCode 33 = PriorkeyCode 34 = NextkeyCode 35 = EndkeyCode 36 = HomekeyCode 37 = LeftkeyCode 38 = UpkeyCode 39 = RightkeyCode 40 = DownkeyCode 41 = SelectkeyCode 42 = PrintkeyCode 43 = ExecutekeyCode 45 = InsertkeyCode 46 = DeletekeyCode 47 = HelpkeyCode 48 = 0 equal bracerightkeyCode 49 = 1 exclam onesuperiorkeyCode 50 = 2 quotedbl twosuperiorkeyCode 51 = 3 section threesuperiorkeyCode 52 = 4 dollarkeyCode 53 = 5 percentkeyCode 54 = 6 ampersandkeyCode 55 = 7 slash braceleftkeyCode 56 = 8 parenleft bracketleftkeyCode 57 = 9 parenright bracketrightkeyCode 65 = a AkeyCode 66 = b BkeyCode 67 = c CkeyCode 68 = d DkeyCode 69 = e E EuroSignkeyCode 70 = f FkeyCode 71 = g GkeyCode 72 = h HkeyCode 73 = i IkeyCode 74 = j JkeyCode 75 = k KkeyCode 76 = l LkeyCode 77 = m M mukeyCode 78 = n NkeyCode 79 = o OkeyCode 80 = p PkeyCode 81 = q Q atkeyCode 82 = r RkeyCode 83 = s SkeyCode 84 = t TkeyCode 85 = u UkeyCode 86 = v VkeyCode 87 = w WkeyCode 88 = x XkeyCode 89 = y YkeyCode 90 = z ZkeyCode 96 = KP_0 KP_0keyCode 97 = KP_1 KP_1keyCode 98 = KP_2 KP_2keyCode 99 = KP_3 KP_3keyCode 100 = KP_4 KP_4keyCode 101 = KP_5 KP_5keyCode 102 = KP_6 KP_6keyCode 103 = KP_7 KP_7keyCode 104 = KP_8 KP_8keyCode 105 = KP_9 KP_9keyCode 106 = KP_Multiply KP_MultiplykeyCode 107 = KP_Add KP_AddkeyCode 108 = KP_Separator KP_SeparatorkeyCode 109 = KP_Subtract KP_SubtractkeyCode 110 = KP_Decimal KP_DecimalkeyCode 111 = KP_Divide KP_DividekeyCode 112 = F1keyCode 113 = F2keyCode 114 = F3keyCode 115 = F4keyCode 116 = F5keyCode 117 = F6keyCode 118 = F7keyCode 119 = F8keyCode 120 = F9keyCode 121 = F10keyCode 122 = F11keyCode 123 = F12keyCode 124 = F13keyCode 125 = F14keyCode 126 = F15keyCode 127 = F16keyCode 128 = F17keyCode 129 = F18keyCode 130 = F19keyCode 131 = F20keyCode 132 = F21keyCode 133 = F22keyCode 134 = F23keyCode 135 = F24keyCode 136 = Num_LockkeyCode 137 = Scroll_LockkeyCode 187 = acute gravekeyCode 188 = comma semicolonkeyCode 189 = minus underscorekeyCode 190 = period colonkeyCode 192 = numbersign apostrophekeyCode 210 = plusminus hyphen macronkeyCode 211 =keyCode 212 = copyright registeredkeyCode 213 = guillemotleft guillemotrightkeyCode 214 = masculine ordfemininekeyCode 215 = ae AEkeyCode 216 = cent yenkeyCode 217 = questiondown exclamdownkeyCode 218 = onequarter onehalf threequarterskeyCode 220 = less greater barkeyCode 221 = plus asterisk asciitildekeyCode 227 = multiply divisionkeyCode 228 = acircumflex AcircumflexkeyCode 229 = ecircumflex EcircumflexkeyCode 230 = icircumflex IcircumflexkeyCode 231 = ocircumflex OcircumflexkeyCode 232 = ucircumflex UcircumflexkeyCode 233 = ntilde NtildekeyCode 234 = yacute YacutekeyCode 235 = oslash OobliquekeyCode 236 = aring AringkeyCode 237 = ccedilla CcedillakeyCode 238 = thorn THORNkeyCode 239 = eth ETHkeyCode 240 = diaeresis cedilla currencykeyCode 241 = agrave Agrave atilde AtildekeyCode 242 = egrave EgravekeyCode 243 = igrave IgravekeyCode 244 = ograve Ograve otilde OtildekeyCode 245 = ugrave UgravekeyCode 246 = adiaeresis AdiaeresiskeyCode 247 = ediaeresis EdiaeresiskeyCode 248 = idiaeresis IdiaeresiskeyCode 249 = odiaeresis OdiaeresiskeyCode 250 = udiaeresis UdiaeresiskeyCode 251 = ssharp question backslashkeyCode 252 = asciicircum degreekeyCode 253 = 3 sterlingkeyCode 254 = Mode_switch

使用event对象的keyCode属性判断输入的键值

eg:

复制代码 代码如下:
if(event.keyCode==13)alert("enter!");

键值对应表

A  0X65  U   0X85B  0X66  V   0X86C  0X67  W   0X87D  0X68  X   0X88E  0X69  Y   0X89F  0X70  Z   0X90G  0X71  0   0X48H  0X72  1   0X49I  0X73  2   0X50J  0X74  3   0X51K  0X75  4   0X52L  0X76  5   0X53M  0X77  6   0X54N  0X78  7   0X55O  0X79  8   0X56P  0X80  9   0X57Q  0X81 ESC  0X1BR  0X82 CTRL  0X11S  0X83 SHIFT 0X10T  0X84 ENTER 0XD

如果要使用组合键,则可以判断是否同时按下了这几个键,比如ctrl键、shift键以及alt键的组合使用就可以判断是否多按下了ctrl键、shift键以及alt键

js获取键盘事件:

<script type="text/javascript" language=JavaScript charset="UTF-8">document.onkeydown=function(event){  var e = event || window.event || arguments.callee.caller.arguments[0];  if(e && e.keyCode==27){ // 按 Esc     //要做的事情   }  if(e && e.keyCode==113){ // 按 F2      //要做的事情    }         if(e && e.keyCode==13){ // enter 键     //要做的事情  }}; </script>

只要你定义了这些键的动作,你在浏览器里按下这些键就会响应,兼容目前所有浏览器。

JS判断键盘按键

window.document.onkeydown = disableRefresh;function disableRefresh(evt){evt = (evt) ? evt : window.eventif (evt.keyCode) {  if(evt.keyCode == 116){   //do something  }}}

键码同上例所示。

使用event对象的keyCode属性判断输入的键值

eg:

if(event.keyCode==13){var ieKey=event.keyCode;//获得键值,数字形式的var realkey=String.fromCharCode(ieKey) //将数字形式的键值转化为真实的按键alert(realkey);}

键值对应表同上例所示

希望本文所述对大家JavaScript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表