Js event binding shortcuts with CTRL +k as an example

  • 2020-03-30 04:02:39
  • OfStack

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(" Registered successfully ");}); 
}, 
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){ 
//Access to the key value
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> 
 Test, press ctrl+k You'll find something amazing happens  
</body> 
</html>

Related articles: