`

js绑定键盘快捷键实战

 
阅读更多

下面这个函数用来响应键盘事件,标签相应onkeydown事件后调用这个函数就可以实现按键的转换功能

//设置快捷键绑定
function setShortcutBinding(){
var a = window.event.keyCode; 
if(a==8){//退格键删除内容
   $("QueryForm:short_cut").value='';
   return;
}
var str = String.fromCharCode(a);//将代号转换为字符
var spec='';
//debugger;
if(event.ctrlKey){   //捕获ctrl事件
   spec=spec+"ctrl+";
   event.returnValue=false;
}
if(event.altKey){   //捕获alt事件
   spec=spec+"alt+";
   event.returnValue=false;
}
if(event.shiftKey){//捕获shift事件
   spec=spec+"shift+";
   event.returnValue=false;
}
$("QueryForm:short_cut").value =spec+str; 
event.returnValue=false;
}

html代码:

<f:view>
<h:form id="QueryForm">
   <h:inputText id="short_cut" onkeydown="setShortcutBinding();"></h:inputText>
</h:form>
</f:view>

说明:在html中使用的EL是$("FormName:tagname").xxx,所以有上述的表达式$("QueryForm:short_cut")即是获得了当前的DOM对象

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics