首页 > 代码库 > JS keycode 事件响应

JS keycode 事件响应

<script language="javascript">function keyevent(){if(event.keyCode==13)alert("你按了回车键");}document.onkeydown = keyevent;//注册document.onkeydown事件
</script>

当用户按回车键时,就会执行keyevent()这个函数。可以做回车提交表单,或其它的事件!

keyCode是返回keydown何keyup事件发生的时候按键的代码,以及keypress 事件的Unicode字符;如果要使用组合键,则可以利用event.ctrlKey,event.shiftKey,event .altKey判断是否按下了ctrl键、shift键以及alt键.

常用keycode
key code key code
left 37 up 38
right 39 down 40
1 49 2 50
3 51 a 65
shift 16 ctrl 17
alt 18 enter 13

image

image

image

示例:

<!DOCTYPE html>
<html>
<head>
    <title>键盘事件keycode</title>
</head>
<body>
    left&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;up&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;down<br />
    ctrl + up&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + down<br />
    ctrl + 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + 3<br />
    a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctrl + shift + a<br />
    <script type="text/javascript">
        document.onkeydown = function (event) {
            event = event || window.event;
            bCtrlKey = event.ctrlKey;
            bShiftKey = event.shiftKey;
            switch (event.keyCode) {
                case 37:
                    alert("left");
                    break;
                case 38:
                    if (bCtrlKey) {
                        alert("ctrl+up");
                        break;
                    }
                    alert("up");
                    break;
                case 39:
                    alert("right");
                    break;
                case 40:
                    if (bCtrlKey) {
                        alert("ctrl+down");
                        break;
                    }
                    alert("down");
                    break;
                case 49:
                    bCtrlKey && (alert("ctrl+1"));
                    break;
                case 50:
                    bCtrlKey && (alert("ctrl+2"));
                    break;
                case 51:
                    bCtrlKey && (alert("ctrl+3"));
                    break;
                case 65:
                    if (bCtrlKey) {
                        if (bShiftKey) {
                            alert("自定义");
                            break;
                        }
                        alert("全选");
                        break;
                    }
                    alert("a");
                    break;
                case 67:
                    bCtrlKey && (alert("复制"));
                    break;
                case 86:
                    bCtrlKey && (alert("粘贴"));
                    break;
            }
            return false;
        }
    </script>
</body>
</html>

---------------------------------------------------------------------------------------------

<script language="javascript">
     function keyevent(){
     if(event.keyCode==13)
     alert("#$%#%#^^%");
     }
     document.onkeydown = keyevent;
</script>
event.keyCode值为37﹐38﹐39﹐40对应按下的方向键分別是 左﹐上﹐右﹐下
"javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = http://www.mamicode.com/false;");
if(event.keycode==13)就代表按下的是回车键盘
"javascript:if (event.keyCode > 45 && event.keyCode < 57) event.returnValue = http://www.mamicode.com/false;");

----------------------------------------------------------------------------------------------

<参考:http://lfsp.btwlo.com/keycode.html & http://www.cnblogs.com/xingqiba/archive/2012/06/12/2546148.html & http://www.cnblogs.com/kuikui/archive/2012/02/09/2343645.html>

JS keycode 事件响应