js事件綁定快速鍵以ctrl+k為例,jsctrl

來源:互聯網
上載者:User

js事件綁定快速鍵以ctrl+k為例,jsctrl

js代碼

<html> <head> <script type="text/javascript"> window.onload=function(){ HotKeyHandler.Init(); } var HotKeyHandler={ currentMainKey:null, currentValueKey:null, Init:function(){ HotKeyHandler.Register(0,"K",function(){alert("註冊成功");}); }, Register:function(tag,value,func){ var MainKey=""; switch(tag){ case 0: MainKey=17; //Ctrl break; case 1: MainKey=16; //Shift break; case 2: MainKey="18"; //Alt break; } document.onkeyup=function(e){ HotKeyHandler.currentMainKey=null; } document.onkeydown=function(event){ //擷取索引值 var keyCode= event.keyCode ; var keyValue = String.fromCharCode(event.keyCode); if(HotKeyHandler.currentMainKey!=null){ if(keyValue==value){ HotKeyHandler.currentMainKey=null; if(func!=null)func(); } } if(keyCode==MainKey) HotKeyHandler.currentMainKey=keyCode; } } } </script> </head> <body> 測試,按下ctrl+k你就會發現神奇的事情發生了 </body> </html>

js實現頁面快速鍵效果----現在實現了ctrl+enter發送資訊的快速鍵效果了,還想做成用alt+S

if(window.event.altKey&&window.event.keyCode==83)
alert(“你按下的是alt+S”);

我試過了IE6可以,opera不行它把alt鍵給佔用了,你看看IE8行不行
 
js按下鍵盤一個鍵 與 按鍵組合 觸發事件

參考一下吧 ,稍作修改就可以滿足1、2兩個條件
但是IE的鍵盤監聽最多隻能作用於document上(window我試過不行)
如果內嵌了iframe並且你的焦點在iframe上,那麼按鍵無效
<script>
document.onkeydown = function()
{

var oEvent = window.event;
if (oEvent.keyCode == 13 && oEvent.ctrlKey) {
alert("你按下了ctrl+enter");
}
}
</script>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.