我试图ctrl使用 jQuery 找出按键按下的字符,但我无法弄清楚代码。
例如:如果我按ctrl+a那么它应该提醒我ctrl+a被按下。
我正在使用下面的代码
$(document).keypress(function(e) {
if(e.ctrlKey){
var ch = String.fromCharCode(e.keyCode);
alert("key pressed ctrl+"+ch); //gives blank value in ch here, I need to know the character pressed
alert("key pressed ctrl+"+e.keyCode); //this works but gives me ASCII value of the key
}
});
Run Code Online (Sandbox Code Playgroud)
您必须使用keydown事件来可靠地捕获关键代码:
$(document).keydown(function(event) {
console.log(event);
if (!event.ctrlKey){ return true; }
$("#result").text(String.fromCharCode(event.which));
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4955 次 |
| 最近记录: |