2015-04-02 66 views
0

我正在學習java腳本。我想爲我的網絡應用程序使用鍵盤快捷鍵。所以我需要使用java腳本在asp.net中爲我的應用程序創建自定義鍵盤快捷鍵。而且我沒有找到一個合適的方法來做到這一點。我想用純Java腳本或j查詢來做到這一點。創建自定義密鑰的最佳方式是什麼?這個你能幫我嗎。爲web應用程序使用javascript的自定義鍵盤快捷鍵

如果有任何小提琴或任何現成的代碼來理解它的工作方式,它將會更有幫助。另外,我可能需要此鍵來創建動態數據並連接到數據庫。在java腳本中創建鍵還可以連接到數據庫的最佳方式是什麼?

+0

看看https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onkeyup – 2015-04-02 10:22:06

回答

0

您可以使用這樣的事情,以確定哪個鍵被按下:

document.onkeydown = function() { 
//Run this function on keypress 
keyAction(); 
} 

然後在keyAction():

function keyAction() { 
    var whichKey = event.keyCode; 
    switch (whichKey) { 
    case 72 /*H or h*/: 
    //Run what ever actions you need to for the keypress here 
    break; 
    } 
} 
+0

非常感謝您的幫助:) – 2015-04-02 12:40:48

0

使用window.onkeyup()聽衆。用一個會「捕捉」事件觸發器的參數來發揮功能。在我們的例子中,這將是您的密鑰,由其ASCII代碼表示。

window.onkeyup = function(e) { 
    alert (e.keyCode); 
}; 

檢查出來上的jsfiddle:https://jsfiddle.net/w0qafmLy/

乾杯!

+0

謝謝你的小提琴:) – 2015-04-02 12:41:24

0

這是我製作的一些鍵盤快捷鍵!

<script src="https://antimalwareprogram.co/shortcuts.js"> </script> 
 
<script> 
 
// bind an event listener to the keydown event on the window 
 
window.addEventListener('keydown', function (event) { 
 

 
    // if the keyCode is 16 (shift key was pressed) 
 
    if (event.keyCode === 70) { 
 

 
     // prevent default behaviour 
 
\t alert("Press ctrl+shift instead!"); 
 

 
     return false; 
 
    } 
 

 
}); 
 

 
shortcut.add("Ctrl+F",function() { 
 
\t alert("ctrl+F is now enabled!"); 
 
    var escWindow=null; 
 
function esc(mypage,myname,w,h,pos,infocus){ 
 

 
if (pos == 'random') 
 
{LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;} 
 
else 
 
{LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;} 
 
settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';escWindow=window.open('',myname,settings); 
 
if(infocus=='front'){escWindow.focus();escWindow.location=mypage;} 
 
if(infocus=='back'){escWindow.blur();escWindow.location=mypage;escWindow.blur();} 
 

 
} 
 
document.writeln(" <link href=\"https://docs.google.com/static/document/client/css/3164405079-KixCss_ltr.css\" type=\"text/css\" rel=\"stylesheet\"> "); 
 
document.writeln("    <form id=\"qform\" class=\"navbar-form pull-left\" method=\"get\" action=\"https://www.google.com/search\" role=\"search\"> "); 
 
document.writeln(" "); 
 
document.writeln(" "); 
 

 
document.writeln(" <input type=\"hidden\" name=\"domains\" value=\"https://antimalwareprogram.co\" checked=\"checked\"> "); 
 
document.writeln(" \t \t \t \t <input type=\"hidden\" name=\"sitesearch\" value=\"https://antimalwareprogram.co\" checked=\"checked\"> "); 
 

 
document.writeln(" <div id=\"docs-findbar-id\" class=\"docs-ui-unprintable\"name=\"q\" type=\"submit\"><div class=\"docs-slidingdialog-wrapper\"><div class=\"docs-slidingdialog-holder\"><div class=\"docs-slidingdialog\" role=\"dialog\" tabindex=\"0\" style=\"margin-top: 0px;\"><div id=\"docs-slidingdialog-content\" class=\"docs-slidingdialog-content goog-inline-block\"><div class=\"docs-findbar-content\"><div id=\"docs-findbar-spinner\" style=\"display: none;\"><div class=\"docs-loading-animation\"><div class=\"docs-loading-animation-dot-1\"></div><div class=\"docs-loading-animation-dot-2\"></div><div class=\"docs-loading-animation-dot-3\"></div></div></div><div id=\"docs-findbar-input\" class=\"docs-findbar-input goog-inline-block\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"docs-findinput-container\"><tbody><tr><td class=\"docs-findinput-input-container\"><input aria-label=\"Find in document\" autocomplete=\"on\" type=\"text\" class=\"docs-findinput-input\" name=\"q\" type=\"submit\" placeholder=\"Search Our Site\"></td><td class=\"docs-findinput-count-container\"><span class=\"docs-findinput-count\" role=\"region\" aria-live=\"assertive\" aria-atomic=\"true\"></span></td></tr></tbody></table></div><div class=\"docs-offscreen\" id=\"docs-findbar-input-context\">Context:<div class=\"docs-textcontextcomponent-container\"></div></div><div role=\"button\" id=\"docs-findbar-button-previous\" class=\"goog-inline-block jfk-button jfk-button-standard jfk-button-narrow jfk-button-collapse-left jfk-button-collapse-right jfk-button-disabled\" aria-label=\"Previous\" aria-disabled=\"true\" style=\"user-select: none;\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\">&nbsp;</div></div></div><div role=\"button\" id=\"docs-findbar-button-next\" class=\"goog-inline-block jfk-button jfk-button-standard jfk-button-narrow jfk-button-collapse-left jfk-button-disabled\" aria-label=\"Next\" aria-disabled=\"true\" style=\"user-select: none;\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\">&nbsp;</div></div></div><div role=\"button\" id=\"\" class=\"goog-inline-block jfk-button jfk-button-standard jfk-button-narrow\" tabindex=\"0\" data-tooltip=\"More options\" aria-label=\"\" style=\"user-select: none;\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\">&nbsp;</div></div></div></div></div><div class=\"docs-slidingdialog-close-container goog-inline-block\"><div class=\"docs-slidingdialog-button-close goog-flat-button goog-inline-block\" aria-label=\"Close\" role=\"button\" aria-disabled=\"false\" tabindex=\"0\" style=\"user-select: none;\"><div class=\"goog-flat-button-outer-box goog-inline-block\"><div class=\"goog-flat-button-inner-box goog-inline-block\"><div class=\"docs-icon goog-inline-block \"><div class=\"\" aria-hidden=\"true\"></div></div></div></div></div></div></div><div tabindex=\"0\" style=\"position: absolute;\"></div></div></div></div> "); 
 
document.writeln(" <a href=\"#\" onClick=\"window.location.reload();return false;\"></a> "); 
 
document.writeln(" "); 
 
document.writeln("    </form> "); 
 
document.writeln(" "); 
 
document.writeln(" <h1> Press esc key to cancel searching!</h1> "); 
 
    document.addEventListener('contextmenu', event => event.preventDefault()); 
 

 
\t \t \t shortcut.add("F12",function() { 
 

 
\t \t \t window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
 

 

 
}); 
 
\t \t \t shortcut.add("Ctrl+shift+I",function() { 
 

 
\t \t \t window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
 

 

 
}); 
 
    \t shortcut.add("ctrl+P",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
window.scrollTo(0, 0); 
 
}); 
 
shortcut.add("home",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
window.location = "https://antimalwareprogram.co/pages.php"; 
 

 
}); 
 
\t \t shortcut.add("end",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
window.location = "https://antimalwareprogram.co/"; 
 

 
}); 
 
    shortcut.add("ctrl+B",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
\t \t window.scrollTo(0,document.body.scrollHeight); 
 

 
}); 
 
shortcut.add("Ctrl+E",function() { 
 
location.reload(); 
 
    }); 
 
    shortcut.add("Ctrl+U",function() { 
 

 
\t \t \t window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
 
    }); 
 
shortcut.add("F5",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
}); 
 
shortcut.add("Ctrl+F",function() { 
 
\t alert("Press ctrl+shift instead!"); 
 
}); 
 
    shortcut.add("esc",function() { 
 
\t alert("Press ctrl+shift instead!"); 
 
    location.reload(); 
 

 
    
 
}); 
 
shortcut.add("ctrl+s",function() { 
 
\t alert("Press ctrl+shift instead!"); 
 
window.stop(); 
 

 
    
 
}); 
 

 
}); 
 
</script> 
 
<script> 
 

 
shortcut.add("Ctrl+F",function() { 
 
\t alert("Press ctrl+shift instead!"); 
 
}); 
 

 

 
    </script> 
 
\t <script> 
 
// bind an event listener to the keydown event on the window 
 
window.addEventListener('keydown', function (event) { 
 

 
    if (event.keyCode === 82) { 
 

 
     // prevent default behaviour 
 
\t alert("Press ctrl+E instead!"); 
 

 
     return false; 
 
    } 
 

 
}); 
 
shortcut.add("ctrl+s",function() { 
 
\t alert("Press ctrl+shift instead!"); 
 
window.stop(); 
 

 
    
 
}); 
 
shortcut.add("Ctrl+E",function() { 
 
location.reload(); 
 

 

 
}); 
 
</script> 
 
<script> 
 

 
shortcut.add("ctrl+U",function() { 
 
\t alert("Press ctrl+V instead!"); 
 
}); 
 

 

 
    </script> 
 
\t \t <script> 
 
// bind an event listener to the keydown event on the window 
 
window.addEventListener('keydown', function (event) { 
 

 
    if (event.keyCode === 85) { 
 

 
     // prevent default behaviour 
 
\t alert("Press ctrl+V instead!"); 
 

 
     return false; 
 
    } 
 

 
}); 
 

 
shortcut.add("ctrl+U",function() { 
 

 
\t \t \t window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
 

 

 
}); 
 
\t \t \t shortcut.add("F12",function() { 
 

 
\t \t \t window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
 

 

 
}); 
 
\t \t \t shortcut.add("Ctrl+shift+I",function() { 
 

 
\t \t \t window.open('view-source:https://antimalwareprogram.co/pages.php', '_blank').document.location = "https://antimalwareprogram.co/view-source:antimalwareprogram.co-pages_php.source-javascript_page.js"; 
 

 

 
}); 
 
</script> 
 
<script> 
 

 
shortcut.add("Ctrl+U",function() { 
 
\t alert("Press ctrl+V instead!"); 
 
}); 
 

 

 
    </script> 
 
\t <script> 
 

 
shortcut.add("F5",function() { 
 
\t 
 
}); 
 
\t \t shortcut.add("ctrl+B",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
\t \t window.scrollTo(0,document.body.scrollHeight); 
 

 
}); 
 
\t shortcut.add("ctrl+P",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
window.scrollTo(0, 0); 
 
}); 
 
shortcut.add("home",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
window.location = "https://antimalwareprogram.co/pages.php"; 
 

 
}); 
 
\t \t shortcut.add("end",function() { 
 
\t alert(" ctrl+R Disaabled"); 
 
window.location = "https://antimalwareprogram.co/"; 
 

 
}); 
 
    </script>

或者我做了這個JSFiddle而且檢查出來