大红色的将军

javascript监控键盘

            var id=-1;
            document.onkeydown =function(event){
                this.e =event || window.event;
                var code = this.e.keyCode;
                if(code==37){
                    console.log(id);
                    window.clearInterval(id);
                    id = window.setInterval("console.log('你按了左键')","1000");
                }else if(code==38){
                    console.log(id);
                    window.clearInterval(id);
                    id = window.setInterval("console.log('你按了上键')","1000");
                }else if(code==39){
                    console.log(id);
                    window.clearInterval(id);
                    id = window.setInterval("console.log('你按了右键')","1000");
                }else if(code==40){
                    console.log(id);
                    window.clearInterval(id);
                    id = window.setInterval("console.log('你按了下键')","1000");
                }else{
                    console.log(id);
                    window.clearInterval(id);
                    id = window.setTimeout("console.log('你按的不是方向键')","1000");
                }
            }

 

posted @ 2019-02-18 15:25  欧德派  阅读(112)  评论(0编辑  收藏  举报