鼠标和键下/上事件的javascript结合[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试创建键盘+鼠标单击快捷方式来执行代码。

在下面的示例中,当我单击鼠标按钮时,它会将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");
	}
};
javascript mouseevent keyboard-events
1个回答
1
投票

用这个

document.onmousedown = function(e) {
  if (e.shiftKey && e.which == 1) {
  alert("Mouse and Shift was pressed");
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.