JavaScript:检查是否按下了 CTRL 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6806271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: Check if CTRL button was pressed
提问by user324589457
I need to check if the CTRLbutton was pressed while I am clicking on a control on my html page using JavaScript.
CTRL当我使用 JavaScript 单击 html 页面上的控件时,我需要检查按钮是否被按下。
How can I do this?
我怎样才能做到这一点?
回答by Charles Ma
Try looking in the event object.
尝试查看事件对象。
e.g.
例如
document.body.onclick = function (e) {
if (e.ctrlKey) {
alert("ctr key was pressed during the click");
}
}
<p>Click me, and sometimes hold CTRL down!</p>
回答by Arun Prasad E S
I use this and works fine
我使用这个并且工作正常
<a href="" onclick="return Details(event)" ></a>
function Details(event) {
if (event.ctrlKey) {
alert('Ctrl down');
}
}
回答by Sacky San
I did it using cntrlIsPressed global flag; also takes care of select all option using Control + A
我使用 cntrlIsPressed 全局标志做到了;还使用 Control + A 处理全选选项
// Check whether control button is pressed
$(document).keydown(function(event) {
if (event.which == "17")
cntrlIsPressed = true;
else if (event.which == 65 && cntrlIsPressed) {
// Cntrl+ A
selectAllRows();
}
});
$(document).keyup(function() {
cntrlIsPressed = false;
});
var cntrlIsPressed = false;