SoFunction
Updated on 2025-02-28

A relatively complete implementation and application of js client shortcut key management class


//A shortcut key object
function KeyOne(id, keys, dom, isfun, fun,iskeydown) {
= id;
= keys;
= dom;
= isfun;
= fun;
= iskeydown;
}

//Shortcut key management class
var KeyConlor = {};
= new Array();
//Add a shortcut key to bind the focus (when the shortcut key is activated, let the focus fall on the specified id object)
//Instruction: If the value of key is "c, 50", it means a combination of "ctrl" and a key code of 50
// "a,50" means a combination of "alt" and a key code of 50
// "s,50" means a combination of "shift" and a key code of 50
// "50" means a single key with a key code of 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
= function(id, key, dom, iskyedown) {
var keyone = new KeyOne(id, key, dom, false, null, iskyedown);
if ((keyone)) {
(keyone);
} else {
alert("shortcut key"++" has been registered and cannot be registered repeatedly");
return false;
}
};

//Shortcut key binding method (trigger method when shortcut key is fired)
= function(key, fun, iskeydown) {
var keyone = new KeyOne("", key, "", true, fun, iskeydown);
if ((keyone)) {
(keyone)
} else {
alert("Shortcut key:" + + "; has been registered. Repeated registration is invalid");
return false;
}
};

//--Delete a shortcut key
// = function(id) {
// var keyone = new KeyOne(id, "");
// for (var i = 0; i < ; i++) {
// if ( == [i].id) {
// [i] = null;
// }
// }
//};

//--Defend whether the shortcut key is registered repeatedly
= function(keyone) {
for (var i = 0; i < ; i++) {
if ([i].keys == ) {
return false;
}
}
return true;
};
= function() {
for (var i = 0; i < ; i++) {
var keyone = [i];
if (!) continue;
var control = (",")[0];
switch (control) {
case 's':
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == true && == code) {
//Get focus
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;
case 'c':
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == true && == code) {
//Get focus
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;
case 'a':
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == true && == code) {
//Get focus
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
=0;
break;

default:
//Get focus
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == code && == false && == false && == false) {
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;
}
}
};
= function() {
for (var i = 0; i < ; i++) {
var keyone = [i];
if () continue;
var control = (",")[0];
switch (control) {
case 's':
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == true && == code) {
//Get focus
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;
case 'c':
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == true && == code) {
//Get focus
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;
case 'a':
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == true && == code) {
//Get focus
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;

default:
//Get focus
var code = (",").length > 1 ? (",")[1] : (",")[0];
if ( == code && == false && == false && == false) {
if (!) {
().focus();
} else {
();
}
=0;
return false;
}
break;
}
}
};
//Common keyboard code
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]
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

<script src="" type="text/javascript"></script>
<script type="text/javascript">
(+","+"70",function (){alert("haha");},true);
("2","65",document,true);
</script>
</head>
<body>
<form >
<div>
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
</div>
</form>
</body>
</html>