js客户端快捷键管理类的较完整实现和应用

复制代码 代码如下:
//一个快捷键对象
function KeyOne(id, keys, dom, isfun, fun,iskeydown) {
this.id = id;
this.keys = keys;
this.dom = dom;
this.isfun = isfun;
this.fun = fun;
this.isKeydown = iskeydown;
}

//快捷键管理类
var KeyConlor = {};
KeyConlor.list = new Array();
//添加一个快捷键绑定焦点(当快捷键被激发时让焦点落在指定id对象上)
//使用说明key的值如果是“c,50”则表示“ctrl”和键码为50的组合键
// "a,50" 则表示“alt”和键码为50的组合键
// "s,50" 则表示“shift”和键码为50的组合键
// "50" 则表示键码为50的单键(建议使用组合键alt)
//id指的是快捷键对应的焦点对象。
//dom指的是id对象所在的document对象
KeyConlor.addkeyfouse = function(id, key, dom, iskyedown) {
var keyone = new KeyOne(id, key, dom, false, null, iskyedown);
if (KeyConlor.KeyIsOK(keyone)) {
KeyConlor.list.push(keyone);
} else {
alert("快捷键"+keyone.keys+"已经被注册 不能重复注册了");
return false;
}
};

//快捷键绑定方法(当快捷键激发时触发方法)
KeyConlor.addkeyfun = function(key, fun, iskeydown) {
var keyone = new KeyOne("", key, "", true, fun, iskeydown);
if (KeyConlor.KeyIsOK(keyone)) {
KeyConlor.list.push(keyone)
} else {
alert("快捷键:" + keyone.keys + ";已经被注册 .重复注册无效");
return false;
}
};

//--删除一个快捷键
//KeyConlor.removeFouseKey = function(id) {
// var keyone = new KeyOne(id, "");
// for (var i = 0; i < KeyConlor.list.length; i++) {
// if (keyone.id == KeyConlor.list[i].id) {
// KeyConlor.list[i] = null;
// }
// }
//};

//--判断快捷键是不是重复注册
KeyConlor.KeyIsOK = function(keyone) {
for (var i = 0; i < KeyConlor.list.length; i++) {
if (KeyConlor.list[i].keys == keyone.keys) {
return false;
}
}
return true;
};
document.onkeydown = function() {
for (var i = 0; i < KeyConlor.list.length; i++) {
var keyone = KeyConlor.list[i];
if (!keyone.isKeydown) continue;
var control = keyone.keys.split(",")[0];
switch (control) {
case 's':
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.shiftKey == true && event.keyCode == code) {
//获得焦点
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;
case 'c':
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.ctrlKey == true && event.keyCode == code) {
//获得焦点
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;
case 'a':
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.altKey == true && event.keyCode == code) {
//获得焦点
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
event.keyCode=0;
break;

default:
//获得焦点
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.keyCode == code && event.altKey == false && event.ctrlKey == false && event.shiftKey == false) {
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;
}
}
};
document.onkeyup = function() {
for (var i = 0; i < KeyConlor.list.length; i++) {
var keyone = KeyConlor.list[i];
if (keyone.isKeydown) continue;
var control = keyone.keys.split(",")[0];
switch (control) {
case 's':
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.shiftKey == true && event.keyCode == code) {
//获得焦点
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;
case 'c':
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.ctrlKey == true && event.keyCode == code) {
//获得焦点
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;
case 'a':
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.altKey == true && event.keyCode == code) {
//获得焦点
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;

default:
//获得焦点
var code = keyone.keys.split(",").length > 1 ? keyone.keys.split(",")[1] : keyone.keys.split(",")[0];
if (event.keyCode == code && event.altKey == false && event.ctrlKey == false && event.shiftKey == false) {
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone.fun();
}
event.keyCode=0;
return false;
}
break;
}
}
};
//常用键盘码
var keyCodeStr = {
Alt: "a",
Shift: "s",
Ctrl: "c",
Up: "38",
Down: "40",
Left: "37",
Right: "39",
Esc: "27",
Enter: "13",
Backspace: "8",
Delete: "46",
Tab: "9",
CapsLK: "20",
Space: "32"
};
[code]
----------以上是js类-------------------------------

[code]
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script src="KyeControl.js" type="text/Javascript"></script>
<script type="text/Javascript">
KeyConlor.addkeyfun(keyCodeStr.Ctrl+","+"70",function (){alert("哈哈");},true);
KeyConlor.addkeyfouse("2","65",document,true);
</script>
</head>
<body>
<form id="form1" >
<div>
<input type="text" id="2" />
<input type="text" id="3" />
<input type="text" id="4" />
<input type="text" id="5" />
</div>
</form>
</body>
</html>

JavaScript技术js客户端快捷键管理类的较完整实现和应用,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。