按下其他键时检测鼠标单击

Detecting mouse click when other key is pressed

我正在尝试制作游戏,并且正在使用 WASD 控件。有时您需要在移动时单击,但 mouseIsPressedmousePressed()mouseClicked() 未检测到它。

我只需要在按下另一个键时检测点击。

示例代码:

function setup() {
  createCanvas(windowWidth, windowHeight).mousePressed(function() {
    console.log("mousePressed");
  });
}

function draw() {
  if (keyIsPressed) {
    console.log("keyIsPressed");  // if you press a key, then click, this is still the only thing being logged
  }
  if (mouseIsPressed) {
    console.log("mouseIsPressed");
  }
}

function mouseClicked() {
  console.log("mouseClicked");
}
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.js"></script>
  <script src="sketch.js"></script>
</head>
</html>

在下面的代码中,我可以同时看到鼠标和按键

    function setup() {
        createCanvas(100, 100);
        background(51);
    }

    function draw() {
        if (keyIsPressed) {
            if(key=="w"){
                print("w")
            } else if(key=="a"){
                print("a")
            } else if(key=="s"){
                print("s")
            } else if(key=="d"){
                print("d")
            }
            if (mouseIsPressed) {
                print("clic");
            }
        } else if (mouseIsPressed) {
            print("clic")
        }
    }

这适合你吗?

这是另一个演示问题的简单程序:

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function draw() {
    background(255, 0, 0);
  if (keyIsPressed) {
    text("keyIsPressed", 100, 100);
    }
  if (mouseIsPressed) {
    text("mouseIsPressed", 200, 200);
  }
}

其实我觉得这个问题比P5.js更笼统。打开另一个程序(我使用的是基本的文本编辑器)并按住一个键,然后尝试移动鼠标。对我来说,只要按住一个键,我的鼠标就会停止响应。

然后我用谷歌搜索 "holding key prevents mouse from moving",得到了很多结果,包括 this one。事实证明,问题是由使用触控板而不是鼠标引起的。显然,触控板的设置会在按下某个键时禁用它们。

使用 "real" 鼠标效果很好,在你的程序和我的程序中。所以解决办法是要么改变你的触控板设置,要么去买一个鼠标。