首页 > 语言 > JavaScript > 正文

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

2024-05-06 12:37:03
字体:
来源:转载
供稿:网友
代码如下:
//一个快捷键对象
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];
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选