结合鼠标和键按下/按下事件的JavaScript

时间:2019-04-09 17:26:25

标签: javascript mouseevent keyboard-events

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

在下面的示例中,当我单击鼠标按钮时,它将返回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");
	}
};

1 个答案:

答案 0 :(得分:1)

使用此

0 */3 * * *