Js-defined hotkey code

Source: Internet
Author: User
Tags bind eval lowercase modifiers
Tip: you can modify some code before running

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script language="javascript" type="text/javascript">/* ---------------------------------------------------------- The system shortcut key is not blocked. In Safari, if you press the system shortcut key, it will not respond to the custom program. CTRL + S is not a shortcut for safari. You can view ctrl + s to respond, ctrl + a, ctrl + d, and so on. ------------------------------------------------------- */Function Browser () {var d _ = document, n _ = navigator, t _ = this, s _ = screen; var B = n _. appName; var ua = n _. userAgent. toLowerCase (); t _. name = "Unknow"; t _. safari = ua. indexOf ("safari")>-1; // always check for safari & opera t _. opera = ua. indexOf ("opera")>-1; // before ns or ie t _. firefox =! T _. safari & ua. indexOf ('Firefox ')>-1; // check for gecko engine t _. ns =! T _. firefox &&! T _. opera &&! T _. safari & (B = "Netscape"); t _. ie =! T _. opera & (B = "Microsoft Internet Explorer"); t _. name = (t _. ie? "IE": (t _. firefox? "Firefox": (t _. ns? "Netscape": (t _. opera? "Opera": (t _. safari? "Safari": "Unknow");} function JShortCut () {var self; var map ={}; var brw = new Browser (); this. bind = function (SC, func, doSysSc) {map [SC] = func; if (doSysSc = false) map [SC]. doSysShortCut = false;} this. unBind = function (SC) {delete map [SC];} this. listen = function (elm) {if (typeof elm = "string") elm = document. getElementById (elm); if (elm. addEventListener) {// Safari, opera, firefox elm. AddEventListener ("keydown", dispatch, false); elm. addEventListener ("keypress", dispatch, false);} else if (elm. attachEvent) {// IE elm. attachEvent ("onkeydown", dispatch); elm. attachEvent ("onkeypress", dispatch);} else {elm. onkeydown = element. onkeypress = dispatch;} var dispatch = function (evt) {evt = evt | event; if (evt. type = "keydown") {var code = evt. keyCode; if (code = 16 | code = 17 | code = 18) return; keyname = JShortCut. KEYS [code]; var modifiers = ""; if (! Keyname & (evt. altKey | evt. ctrlKey) keyname = Keymap. keyCodeToPrintableChar [code]; if (keyname) {if (evt. altKey) modifiers + = "alt _"; if (evt. ctrlKey) modifiers + = "ctrl _"; if (evt. shiftKey) modifiers + = "shift _";} else {return ;}} else if (evt. type = "keypress") {if (evt. altKey | evt. ctrlKey) return; if (evt. charCode! = Undefined & evt. charCode = 0) return; var code = evt. charCode | evt. keyCode; keyname = String. fromCharCode (code); var lowercase = keyname. toLowerCase (); if (keyname! = Lowercase) {keyname = lowercase; // Use the lowercase form of the name modifiers = "shift _"; // and add the shift modifier .}} var export cut = modifiers + keyname; var target = evt. srcElement | evt.tar get; var func = map [shortCut]; if (typeof func = "function") {func (target, shortCut, evt); if (func. doSysShortCut = false) {switch (brw. name) {case "IE": evt. returnValue = false; evt. keyCode = 0; break; case "Firefox": evt. preventDefault (); break; case "Opera": evt. returnValue = false; evt. preventDefault (); break; case "Safari": evt. preventDefault (); evt. returnValue = false; // for (o in evt) // document. write (o, "", typeof (eval ("evt. "+ o)," = ", eval (" evt. "+ o ),"<br />") ;}}// Return false ;}} JShortCut. KEYS = {48: "0", 49: "1", 50: "2", 51: "3", 52: "4", 53: "5 ", 54: "6", 55: "7", 56: "8", 57: "9", 59: ";", 61: "=", 65: "a", 66: "B", 67: "c", 68: "d", 69: "e", 70: "f", 71: "g ", 72: "h", 73: "I", 74: "j", 75: "k", 76: "l", 77: "m", 78: "n", 79: "o", 80: "p", 81: "q", 82: "r", 83: "s", 84: "t ", 85: "u", 86: "v", 87: "w", 88: "x", 89: "y", 90: "z", 107: "+", 109: "-", 110 :". ", 188:", ", 190 :". ", 191:"/", 192:" '", 219:" [", 220:" \ ", 221:"] ", 222 :"\"", 8: "backspace", 9: "tab", 13: "return", 19: "pause", 27: "escape", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home", 37: "left", 38: "up", 39: "right ", 40: "down", 44: "printscreen", 45: "insert", 46: "delete", 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8", 120: "f9 ", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scrolllock "};</script></head><body><div id="aa"></div><p>In Opera, system shortcuts are not blocked.<br />In Safari, if you press the system shortcut key, it will not respond to the custom program.<br />CTRL + S is not a shortcut for safari. You can view ctrl + s to respond, ctrl + a, ctrl + d, and so on.</p><p>Function doSys (target, shortCut, evt ){<br />Document. getElementById ("aa"). innerHTML = shortCut;<br /> }</p><p>Var SC = new JShortCut ();<br />SC. bind ("ctrl_a", doSys, false );<br />SC. bind ("ctrl_n", doSys, false );<br />SC. bind ("ctrl_p", doSys, false );<br />SC. bind ("ctrl_o", doSys, false );<br />SC. bind ("ctrl_s", doSys, false );<br />SC. bind ("ctrl_w", doSys, false );<br />SC. bind ("alt_f4", doSys, false );<br />SC. bind ("ctrl_f", doSys, false );<br />SC. bind ("ctrl_y", doSys, false );<br />SC. bind ("f1", doSys, false );<br />SC. bind ("f5", doSys, false );<br />SC. bind ("a", doSys );<br />SC. listen (document );</p></body></html><script language="javascript" type="text/javascript">Function doSys (target, shortCut, evt) {document. getElementById ("aa "). innerHTML = shortCut;} var SC = new JShortCut (); SC. bind ("ctrl_a", doSys, false); SC. bind ("ctrl_n", doSys, false); SC. bind ("ctrl_p", doSys, false); SC. bind ("ctrl_o", doSys, false); SC. bind ("ctrl_s", doSys, false); SC. bind ("ctrl_w", doSys, false); SC. bind ("alt_f4", doSys, false); SC. bind ("ctrl_f", doSys, false); SC. bind ("ctrl_y", doSys, false); SC. bind ("f1", doSys, false); SC. bind ("f5", doSys, false); SC. bind ("a", doSys); SC. listen (document );</script>
Tip: you can modify some code before running

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.