Complete implementation and application of JS client Shortcut Key Management

Source: Internet
Author: User

CopyCode 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
VaR keyconlor = {};
Keyconlor. List = new array ();
// Add a shortcut key to bind the focus (set the focus to the specified ID object when the shortcut key is activated)
// If the key value of the instruction is "C, 50", the key combination is "Ctrl" and the key code is 50.
// "A, 50" indicates the combination of "Alt" and 50 key codes.
// "S, 50" indicates the combination of "shift" and 50 key codes.
// "50" indicates a single key with a 50 key code (ALT is recommended)
// ID indicates the focal point object corresponding to the shortcut key.
// Dom indicates the Document Object of the ID object.
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 cannot be registered again ");
Return false;
}
};

// Shortcut Key Binding 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;
//}
//}
//};

// -- Judge whether the shortcut key is registered repeatedly
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 ){
// Obtain the 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 ){
// Obtain the 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 ){
// Obtain the focus
If (! Keyone. isfun ){
Keyone. Dom. getelementbyid (keyone. ID). Focus ();
} Else {
Keyone. Fun ();
}
Event. keycode = 0;
Return false;
}
Event. keycode = 0;
Break;

Default:
// Obtain the 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 ){
// Obtain the 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 ){
// Obtain the 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 ){
// Obtain the focus
If (! Keyone. isfun ){
Keyone. Dom. getelementbyid (keyone. ID). Focus ();
} Else {
Keyone. Fun ();
}
Event. keycode = 0;
Return false;
}
Break;

Default:
// Obtain the 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;
}
}
};
// Common keyboard Codes
VaR keycodestr = {
ALT: "",
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]
---------- The above are JS classes -------------------------------

[Code]
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>














Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.