1 /**
  2  * 快捷键设置
  3  * @name vui.hotkey
  4  * @constructor
  5  * @author putaoshu@126.com
  6  * @date 2012-04-18
  7  * @param {String} event keydown 其它为keypress|keyup
  8  * @param {String} key 键值esc,enter...
  9  * @param {String} name 快捷键对应回调时会用的名称
 10  * @param {Function} callback 回调函数
 11  * @example
 12 vui.hotkey({event:'keydown',key:'enter',name:'hotkeyName'},function(){
 13 	 alert('todo');
 14 });
 15  */
 16 //所有回调函数集
 17 vui.hotkeyArray = {};
 18 
 19 vui.hotkey = function(opt,callback){
 20 	opt.event = opt.event.toLowerCase();
 21 	opt.key = opt.key.toLowerCase();
 22 	var typeReg=/^keypress|keydown|keyup$/;
 23 	if(!typeReg.test(opt.event)){
 24 		return;
 25 	};
 26 
 27 	//特殊键的对照
 28 	var keyMap = { 
 29 		27: 'esc', 9: 'tab', 32:'space', 13: 'enter', 8:'backspace', 145: 'scrollclock', 
 30 		20: 'capslock', 144: 'numlock', 19:'pause', 45:'insert', 36:'home', 46:'delete',
 31 		35:'end', 33: 'pageup', 34:'pagedown', 37:'left', 38:'up', 39:'right',40:'down', 
 32 		112:'f1',113:'f2', 114:'f3', 115:'f4', 116:'f5', 117:'f6', 118:'f7', 119:'f8', 
 33 		120:'f9', 121:'f10', 122:'f11', 123:'f12', 191: '/', 17:'ctrl', 16:'shift',
 34 		109:'-',107:'=',219:'[',221:']',220:'\\',222:'\'',187:'=',188:',',189:'-',190:'.',191:'/',
 35 		96: '0', 97:'1', 98: '2', 99: '3', 100: '4', 101: '5', 102: '6', 103: '7',
 36 		104: '8', 105: '9', 106: '*', 110: '.', 111 : '/',
 37 		65:'a',66:'b',67:'c',68:'d',69:'e',70:'f',71:'g',72:'h',73:'i',74:'j',75:'k',76:'l',
 38 		77:'m',78:'n',79:'o',80:'p',81:'q',82:'r',83:'s',84:'t',85:'u',86:'v',87:'w',88:'x',89:'y',90:'z'
 39 	};
 40 
 41 	//绑定的函数入库
 42 	vui.hotkeyArray[opt.name] = callback;
 43 
 44 	$(document).bind(opt.event,function(event){
 45 		var whichNum=0;
 46 		$.each(keyMap,function(i){
 47 			if (keyMap[i]==opt.key){
 48 				whichNum = i;
 49 				//vui.hotkeyArray[i]();
 50 				return false;
 51 			}
 52 		})
 53 
 54 		if(event.which == whichNum){
 55 			vui.hotkeyArray[opt.name]();
 56 			//$.each(vui.hotkeyArray,function(i){
 57 			//	//vui.hotkeyArray[i]();
 58 			//	this.apply();
 59 			//})
 60 		};
 61 
 62 	});
 63 }
 64 
 65 //移除hotkey
 66 vui.unhotkey = function(hotkeyName){
 67 	//从回调函数集移除
 68 	delete vui.hotkeyArray[hotkeyName];
 69 }
 70 
 71