Home  >  Article  >  Web Front-end  >  A more complete implementation and application of js client shortcut key management class_javascript skills

A more complete implementation and application of js client shortcut key management class_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:25:261225browse
Copy code The code is as follows:

//A shortcut key object
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;
}

//Shortcut key management Class
var KeyConlor = {};
KeyConlor.list = new Array();
//Add a shortcut key binding focus (let the focus fall on the specified id object when the shortcut key is activated)
//Instructions for use: If the value of key is "c,50", it means "ctrl" and the key code is 50.
// "a,50" means "alt" and the key code is 50. The key combination
// "s,50" means "shift" and the key combination with key code 50
// "50" means the single key with key code 50 (it is recommended to use the key combination alt)
//id refers to the focus object corresponding to the shortcut key.
//dom refers to the document object where the id object is located
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("Shortcut key" keyone.keys "already Registered and cannot be re-registered");
return false;
}
};

//Shortcut key binding method (method triggered when the shortcut key is activated)
KeyConlor .addkeyfun = function(key, fun, iskeydown) {
var keyone = new KeyOne("", key, "", true, fun, iskeydown);
if (KeyConlor.KeyIsOK(keyone)) {
KeyConlor.list.push(keyone)
} else {
alert("Shortcut key: " keyone.keys "; Already registered. Repeated registration is invalid");
return false;
}
};

//--Delete a shortcut key
//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;
// }
// }
//};

//--Determine whether the shortcut key is a duplicate registration
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) {
//Get focus
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) {
//Get focus
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) {
//Get focus
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone .fun();
}
event.keyCode=0;
return false;
}
event.keyCode=0;
break;

default:
//Get focus
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) {
//Get focus
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) {
/ /Get focus
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) {
//Get focus
if (!keyone.isfun) {
keyone.dom.getElementById(keyone.id).focus();
} else {
keyone. fun();
}
event.keyCode=0;
return false;
}
break;

default:
//Get focus
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]


















Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn