兼容各个浏览器实现键盘响应事件

前端开发中, 响应键盘事件是经常用到的, 而且要求兼容各个浏览器, 方便用到是直接参考就可以了,下面就用 JS 实现一个兼容各个浏览器的通用响应键盘事件的函数:


function xKeyEvent(e){   //通用的兼容各个浏览器的响应键盘事件函数
//FUNCTION: do the EVENT of KEY 
var e = e || window.event;  //事件处理
switch(e.keyCode | e.which | e.charCode)  //按键 ASCII 码值
    {
     case 8: //响应退格键
          {
          //...x operation...		  
          break;
          }

     case 9: //响应Tab键
          {
          //...x operation...		 
          break;
          }

     case 16: //响应Shift键
          {
          //...x operation...		
          break;
          }

     case 17: //响应Ctrl键
          {
          //...x operation...		
          break;
          }

     case 18: //响应Alt键
          {
          //...x operation...		 
          break;
          }

     case 20: //响应Caps Lock键
          {
          //...x operation...		
          break;
          }

     case 27: //响应Esc键
          {
          //...x operation...		  
          break;
          }

     case 32: //响应空格键
          {
          //...x operation...		 
          break;
          }

     case 33: //响应PageUp键
          {
          //...x operation...		 
          break;
          }

     case 34: //响应PageDown键
          {
          //...x operation...		  
          break;
          }

     case 35: //响应End键
          {
          //...x operation...		 
          break;
          }

     case 36: //响应Home键
          {
          //...x operation...		 
          break;
          }
		  
     case 37: //向左方向键
          {
          //...x operation...		
          break;
          }

     case 38: //向上方向键
          {
          //...x operation...		 
          break;
          }

     case 39: //向右方向键
          {
          //...x operation...		 
          break;
          }

     case 40: //向下方向键
          {
          //...x operation...		 
          break;
          }

     case 45: //响应Insert键
          {
          //...x operation...		  
          break;
          }

     case 46: //响应Delete键
          {
          //...x operation...		  
          break;
          }
		  
     case 48: //响应 0 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		  
          break;
          }
		  
     case 49: //响应 1 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		 
          break;
          }
		  
     case 50: //响应 2 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		
          break;
          }
		  
     case 51: //响应 3 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		 
          break;
          }
		  
     case 52: //响应 4 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		  
          break;
          }
		  
     case 53: //响应 5 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		  
          break;
          }
		  
     case 54: //响应 6 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		  
          break;
          }

     case 55: //响应 7 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		  
          break;
          }
		  
     case 56: //响应 8 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		 
          break;
          }
		  
     case 57: //响应 9 键, // 0 --9 对应的ASCII码值 48 -- 57
          {
          //...x operation...		 
          break;
          }

     case 65: //响应 a 键, //a -- z, 对应的ASCII码值 65 -- 90
          {
          //...x operation...		
          break;
          }
		  
     case 90: //响应 z 键, //a -- z, 对应的ASCII码值 65 -- 90
          {
          //...x operation...
		 
          break;
          }

     case 91: //响应左WIN键
          {
          //...x operation...		  
          break;
          }

     case 92: //响应右WIN键
          {
          //...x operation...		 
          break;
          }
		  
     case 96: //小键盘区0	//小键盘区0--9对应键值 96--105
          {
          //...x operation...		
          break;
          }
		  
     case 105: //小键盘区9	//小键盘区0--9对应键值 96--105
          {
          //...x operation...		
          break;
          }

     case 112: //响应F1键 //码112--123对应F1--F12
          {
          //...x operation...		  
          break;
          }
		  
     case 113: //响应F2键 // 码112--123对应F1--F12
          {
          //...x operation...		 
          break;
          }
		  
     case 114: //响应F3键 // 码112--123对应F1--F12
          {
          //...x operation...		  
          break;
          }
		  
     case 115: //响应F4键 //码112--123对应F1--F12
          {
          //...x operation...		 
          break;
          }
		  
     case 116: //响应F5键 //码112--123对应F1--F12
          {
          //...x operation...		 
          break;
          }
		  
     case 117: //响应F6键 // 码112--123对应F1--F12
          {
          //...x operation...		 
          break;
          }

     case 118: //响应F7键 // 码112--123对应F1--F12
          {
          //...x operation...		
          break;
          }
		  
     case 119: //响应F8键 //码112--123对应F1--F12
          {
          //...x operation...		 
          break;
          }
		  
     case 120: //响应F9键 //码112--123对应F1--F12
          {
          //...x operation...		
          break;
          }
		  
     case 121: //响应F10键 //码112--123对应F1--F12
          {
          //...x operation...		
          break;
          }
		  
     case 122: //响应F11键 //码112--123对应F1--F12
          {
          //...x operation...		 
          break;
          }
		  
     case 123: //响应F12键 //码112--123对应F1--F12
          {
          //...x operation...		
          break;
          }

     case 144: //响应 NumLock 键 
          {
          //...x operation...		 
          break;
          }

     case 145: //响应 ScrollLock 键 
          {
          //...x operation...		
          break;
          }

     case 186: //响应 ScrollLock 键 
          {
          //...x operation...		
          break;
          }
		  
     case 187: //响应 等号 键 
          {
          //...x operation...		
          break;
          }

     case 188: //响应 逗号 键 
          {
          //...x operation...		
          break;
          }

     case 189: //响应 减号 键 
          {
          //...x operation...		
          break;
          }
		  
     case 190: //响应 点号 键 
          {
          //...x operation...		
          break;
          }
		  
     case 191: //响应 正斜杠 键 
          {
          //...x operation...		  
          break;
          }

     case 192: //响应 (前导点号)` 键 
          {
          //...x operation...		
          break;
          }

     case 219: //响应 [ 键 
          {
          //...x operation...		
          break;
          }
		  
     case 220: //响应 \ 键 
          {
          //...x operation...		
          break;
          }

     case 221: //响应 ] 键 
          {
          //...x operation...		
          break;
          }

     case 13: //响应回车键
          {
          //...x operation...		
          break;
          }
     default:
	  //...default operation...	
    }
}



2, 引用:
onKeyPress="javascript:xKeyEvent(event);">




你可能感兴趣的:(JavaScript,Browser/浏览器)