Javascript jQuery-添加热键
当我在键盘上单击“1”时,我使用此代码复制段落:Javascript jQuery-添加热键,javascript,jquery,html,css,Javascript,Jquery,Html,Css,当我在键盘上单击“1”时,我使用此代码复制段落: $(document).keypress(function (e) { if (e.which == 49) { var $temp = $("<input>"); $("body").append($temp); $temp.val($('.one').children('p').text()).select(); document.execCommand("copy"); $temp.rem
$(document).keypress(function (e) {
if (e.which == 49) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($('.one').children('p').text()).select();
document.execCommand("copy");
$temp.remove();
}
});
$(文档)。按键(功能(e){
如果(e.which==49){
变量$temp=$(“”);
$(“正文”)。追加($temp);
$temp.val($('.one').children('p').text()).select();
文件。执行命令(“副本”);
$temp.remove();
}
});
但我想添加真正的热键,例如ctrl+alt+1,您可以使用,如下所示
下面的代码所做的是,在keydown
上,它将对象keys
中该键的值设置为true。它还使用if语句检查是否按下了这3个键。如果是这样的话,它会有所作为
17
、18
和49
是用于CtrlShift 1的
var-keys={};
$(文档).keydown(函数(e){
键[e.which]=true;
如果(键[17]&&键[18]&&键[49]){//Ctrl+Alt+1按该顺序排列
控制台日志(“按下”);
}
});
$(文档).keyup(函数(e){
删除键[e.which];
});代码>
我喜欢这个,你可以使用shiftkey如果你想成为e.shiftkey
你可以很容易地使用jQuery或不使用jQuery(我已经包括了两个版本)。只要记住,一旦您知道自己对按键感兴趣,就要使用evt.preventDefault()阻止默认行为,以防用户浏览器的热键与您要使用的热键匹配
这是jQuery版本
$(文档).keydown(函数(evt){
如果(!evt.ctrlKey | | |!evt.altKey | | evt.which<48 | | evt.which>57){
返回;
}
evt.preventDefault();
开关(evt.WHIT){
案例49:
控制台日志(“热键1”);
返回;
案例50:
控制台日志(“热键2”);
返回;
案例51:
控制台日志(“热键3”);
返回;
案例52:
控制台日志(“热键4”);
返回;
案例53:
控制台日志(“热键5”);
返回;
案例54:
控制台日志(“热键6”);
返回;
案例55:
控制台日志(“热键7”);
返回;
案例56:
控制台日志(“热键8”);
返回;
案例57:
控制台日志(“热键9”);
返回;
案例48:
console.log(“热键0”);
返回;
}
});
这是本机js版本
window.addEventListener(“键下”),函数(evt){
如果(!evt.altKey | | |!evt.ctrlKey | | evt.which<48 | | evt.which>57){
返回;
}
evt.preventDefault();
开关(evt.WHIT){
案例49:
控制台日志(“热键1”);
返回;
案例50:
控制台日志(“热键2”);
返回;
案例51:
控制台日志(“热键3”);
返回;
案例52:
控制台日志(“热键4”);
返回;
案例53:
控制台日志(“热键5”);
返回;
案例54:
控制台日志(“热键6”);
返回;
案例55:
控制台日志(“热键7”);
返回;
案例56:
控制台日志(“热键8”);
返回;
案例57:
控制台日志(“热键9”);
返回;
案例48:
console.log(“热键0”);
返回;
}
},假);
您可能需要查看以下内容:
$(document).keydown(function(e) {
if (e.ctrlKey && e.altKey && e.which == 49) { // Ctrl + Alt + 1 in that order
console.log("pressed");
}
});
$(document).keydown(function(evt) {
if (!evt.ctrlKey || !evt.altKey || evt.which < 48 || evt.which >57) {
return;
}
evt.preventDefault();
switch(evt.which) {
case 49:
console.log("hotkey 1");
return;
case 50:
console.log("hotkey 2");
return;
case 51:
console.log("hotkey 3");
return;
case 52:
console.log("hotkey 4");
return;
case 53:
console.log("hotkey 5");
return;
case 54:
console.log("hotkey 6");
return;
case 55:
console.log("hotkey 7");
return;
case 56:
console.log("hotkey 8");
return;
case 57:
console.log("hotkey 9");
return;
case 48:
console.log("hotkey 0");
return;
}
});
window.addEventListener("keydown", function (evt) {
if(!evt.altKey || !evt.ctrlKey || evt.which < 48 || evt.which > 57) {
return;
}
evt.preventDefault();
switch(evt.which) {
case 49:
console.log("hotkey 1");
return;
case 50:
console.log("hotkey 2");
return;
case 51:
console.log("hotkey 3");
return;
case 52:
console.log("hotkey 4");
return;
case 53:
console.log("hotkey 5");
return;
case 54:
console.log("hotkey 6");
return;
case 55:
console.log("hotkey 7");
return;
case 56:
console.log("hotkey 8");
return;
case 57:
console.log("hotkey 9");
return;
case 48:
console.log("hotkey 0");
return;
}
}, false);