首页 > 代码库 > 使用js 设置组合快捷键

使用js 设置组合快捷键

使用js 设置组合快捷键

  使用js设置快捷键的本质就是获得你要设置的这个键的keyCode 的值,如果要加ctrl,alt,shift,那么就添加一个ctrlkey,altKey,shiftKey来判断下,所以关键在于获取keycode的值。

  (1) 设置 ctrl +enter 提交  

1 if (e.ctrlKey && e.keyCode == 13){  2    return submit();  3 }

  (2) 设置  Alt+ 方向键 ←提交

1 if (e.altKey&& e.keyCode == 37){  2     return submit(); 3 }

  (3) 设置  shift+F10 提交

1 if (e.shiftKey&& e.keyCode == 37){   2    return submit();3 4 }

  (4) 设置enter 提交

1 if (e.keyCode == 13){2  return submit();   3 }

  一些常见的快捷键的KeyCode:

1 keycode    8 = BackSpace BackSpace 2   keycode    9 = Tab Tab 3   keycode   12 = Clear 4   keycode   13 = Enter 5   keycode   16 = Shift_L 6   keycode   17 = Control_L 7   keycode   18 = Alt_L 8   keycode   19 = Pause 9   keycode   20 = Caps_Lock10   keycode   27 = Escape Escape11   keycode   32 = space space12   keycode   33 = Prior13   keycode   34 = Next14   keycode   35 = End15   keycode   36 = Home16   keycode   37 = Left17   keycode   38 = Up18   keycode   39 = Right19   keycode   40 = Down20   keycode   41 = Select21   keycode   42 = Print22   keycode   43 = Execute23   keycode   45 = Insert24   keycode   46 = Delete25   keycode   47 = Help

<script type="text/javascript">

//当onkeydown 事件发生时调用hotkey函数
        $("#CarNo").keydown(function (e) {
            if (e.keyCode == 13)
            {
               alert("你按了键盘ENTER.");
            }
        });

</script>

使用js 设置组合快捷键