js事件绑定快捷键以ctrl+k为例

时间:2022-03-07 19:38:37

js代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<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>