Home > Web Front-end > JS Tutorial > body text

js event binding shortcut key using ctrl k as an example_javascript skills

WBOY
Release: 2016-05-16 16:34:49
Original
1600 people have browsed it

js code

<html> 
<head> 
<script type="text/javascript"> 
window.onload=function(){ 
HotKeyHandler.Init(); 
} 
var HotKeyHandler={ 
currentMainKey:null, 
currentValueKey:null, 
Init:function(){ 
HotKeyHandler.Register(0,"K",function(){alert("注册成功");}); 
}, 
Register:function(tag,value,func){ 
var MainKey=""; 
switch(tag){ 
case 0: 
MainKey=17; //Ctrl 
break; 
case 1: 
MainKey=16; //Shift 
break; 
case 2: 
MainKey="18"; //Alt 
break; 
} 
document.onkeyup=function(e){ 
HotKeyHandler.currentMainKey=null; 
} 

document.onkeydown=function(event){ 
//获取键值 
var keyCode= event.keyCode ; 
var keyValue = String.fromCharCode(event.keyCode); 

if(HotKeyHandler.currentMainKey!=null){ 
if(keyValue==value){ 
HotKeyHandler.currentMainKey=null; 
if(func!=null)func(); 
} 
} 
if(keyCode==MainKey) 
HotKeyHandler.currentMainKey=keyCode; 
} 
} 
} 
</script> 
</head> 
<body> 
测试,按下ctrl+k你就会发现神奇的事情发生了 
</body> 
</html>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template