首页 > 编程 > JavaScript > 正文

Atitit.js的键盘按键事件捆绑and事件调度

2019-11-20 10:18:30
字体:
来源:转载
供稿:网友

 1. Best的方法还是

objEvtMap[ id+evt ]=function(evt,element)

2. Event bind

function bindEvent_4_dataBlock(){var arr=$("#table1 li");arr.each(function(index, element) {$(element).keypress(function(){alert('keypress');});var id="data_aid_"+index;$(element).attr("id","data_aid_"+index);objEvtMap[ id ]=function(evt,element){if(evt=="down")key_press_hadler($(element));if(evt=="up")key_press_hadler4up($(element));if(evt=="left"){};if(evt=="rit"){}};});}

3. Event dispatch

document.onkeydown=function(event){var e = event || window.event || arguments.callee.caller.arguments[0];alert("keycode:"+e.keyCode);if(e && e.keyCode==40 ){//下//alert('40=下键,39=右键');var now=$(".selected");var id=$(now).attr("id");var func=objEvtMap[id ];func("down",now);//key_press_hadler(now);return false;}

js中获取键盘事件

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

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

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

keyCode = BackSpace BackSpacekeyCode = Tab TabkeyCode = ClearkeyCode = EnterkeyCode = Shift_LkeyCode = Control_LkeyCode = Alt_LkeyCode = PausekeyCode = Caps_LockkeyCode = Escape EscapekeyCode = spacekeyCode = PriorkeyCode = NextkeyCode = EndkeyCode = HomekeyCode = LeftkeyCode = UpkeyCode = RightkeyCode = DownkeyCode = SelectkeyCode = PrintkeyCode = ExecutekeyCode = InsertkeyCode = DeletekeyCode = HelpkeyCode = equal bracerightkeyCode = exclam onesuperiorkeyCode = quotedbl twosuperiorkeyCode = section threesuperiorkeyCode = dollarkeyCode = percentkeyCode = ampersandkeyCode = slash braceleftkeyCode = parenleft bracketleftkeyCode = parenright bracketrightkeyCode = a AkeyCode = b BkeyCode = c CkeyCode = d DkeyCode = e E EuroSignkeyCode = f FkeyCode = g GkeyCode = h HkeyCode = i IkeyCode = j JkeyCode = k KkeyCode = l LkeyCode = m M mukeyCode = n NkeyCode = o OkeyCode = p PkeyCode = q Q atkeyCode = r RkeyCode = s SkeyCode = t TkeyCode = u UkeyCode = v VkeyCode = w WkeyCode = x XkeyCode = y YkeyCode = z ZkeyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_ KP_keyCode = KP_Multiply KP_MultiplykeyCode = KP_Add KP_AddkeyCode = KP_Separator KP_SeparatorkeyCode = KP_Subtract KP_SubtractkeyCode = KP_Decimal KP_DecimalkeyCode = KP_Divide KP_DividekeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = FkeyCode = Num_LockkeyCode = Scroll_LockkeyCode = acute gravekeyCode = comma semicolonkeyCode = minus underscorekeyCode = period colonkeyCode = numbersign apostrophekeyCode = plusminus hyphen macronkeyCode =keyCode = copyright registeredkeyCode = guillemotleft guillemotrightkeyCode = masculine ordfemininekeyCode = ae AEkeyCode = cent yenkeyCode = questiondown exclamdownkeyCode = onequarter onehalf threequarterskeyCode = less greater barkeyCode = plus asterisk asciitildekeyCode = multiply divisionkeyCode = acircumflex AcircumflexkeyCode = ecircumflex EcircumflexkeyCode = icircumflex IcircumflexkeyCode = ocircumflex OcircumflexkeyCode = ucircumflex UcircumflexkeyCode = ntilde NtildekeyCode = yacute YacutekeyCode = oslash OobliquekeyCode = aring AringkeyCode = ccedilla CcedillakeyCode = thorn THORNkeyCode = eth ETHkeyCode = diaeresis cedilla currencykeyCode = agrave Agrave atilde AtildekeyCode = egrave EgravekeyCode = igrave IgravekeyCode = ograve Ograve otilde OtildekeyCode = ugrave UgravekeyCode = adiaeresis AdiaeresiskeyCode = ediaeresis EdiaeresiskeyCode = idiaeresis IdiaeresiskeyCode = odiaeresis OdiaeresiskeyCode = udiaeresis UdiaeresiskeyCode = ssharp question backslashkeyCode = asciicircum degreekeyCode = sterlingkeyCode = Mode_switch

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

eg:if(event.keyCode==)alert(“enter!”);

键值对应表

A  X  U   XB  X  V   XC  X  W   XD  X  X   XE  X  Y   XF  X  Z   XG  X     XH  X     XI  X     XJ  X    XK  X    XL  X    XM  X     XN  X     XO  X     XP  X     XQ  X ESC  XBR  X CTRL  XS  X SHIFT XT  X ENTER XD

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

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