I'm trying to make a game, and I'm using WASD controls. You sometimes need to click while moving, but mouseIsPressed
, mousePressed()
, and mouseClicked()
aren't detecting it.
I just need to detect a click while another key is being pressed.
EXAMPLE CODE:
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>
Here's another simple program that demonstrates the problem:
function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
background(255, 0, 0);
if (keyIsPressed) {
text("keyIsPressed", 100, 100);
}
if (mouseIsPressed) {
text("mouseIsPressed", 200, 200);
}
}
Actually, I think this problem is more general than P5.js. Open another program (I used a basic text editor) and hold a key down, then try to move the mouse. For me, my mouse stopped responding whenever I was holding down a key.
Then I googled "holding key prevents mouse from moving" which had a bunch of results, including this one . It turns out that the problem is caused by using a trackpad instead of a mouse. Apparently trackpads have settings that disable them when a key is pressed.
Using a "real" mouse works perfectly, in your program and in mine. So the solution is to either change your trackpad settings, or to go get a mouse.
In the following code, i can see mouse and key simultaneously
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")
}
}
is this working for you ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.