这个问题在这里已有答案:
我正在尝试创建键盘+鼠标单击快捷方式来执行代码。
在下面的示例中,当我单击鼠标按钮时,它会将var mouseDown返回为1。
然后,如果mouseDown等于1并按下键盘键移动,我希望显示警告消息。
键盘+鼠标单击事件失败:
var mouseDown = 0;
document.onmousedown = function(e) {
if (e.which == 1) { //Left mouse button on Mac
mouseDown = 1;}
else {
mouseDown = 0;}
};
if (mouseDown == 1) {
document.onkeyup = function(e) {
if (e.which == 16) { //Shift key on Mac
alert("Shift + mouse was pressed");
mouseDown = 0;
}
};
}
单击鼠标单击事件有效:
document.onmousedown = function(e) {
if (e.which == 1) {
alert("Mouse was pressed");
}
};
Shift键事件单独工作:
document.onkeyup = function(e) {
if (e.which == 16) {
alert("Shift was pressed");
}
};
用这个
document.onmousedown = function(e) {
if (e.shiftKey && e.which == 1) {
alert("Mouse and Shift was pressed");
}
};