Home 09: Mouse buttons
Content
Cancel

09: Mouse buttons

Beside the positon, your mouse has some buttons to click too, so let us use them.

To know when your mouse button is pressed you can create a function with the name mousePressed. Like with the draw function, this name can not be changed when using p5.js.

1
2
3
function mousePressed() {
  console.log("The mouse button was pressed");
}

If you want to know when the use released the mouse button, you have to create a function name mouseReleased.

1
2
3
function mouseReleased() {
  console.log("The mouse button was released");
}

To know when the user has clicked the mouse button, you can use a function name mouseClicked. Clicked means that the user has pressed and afterwards released the mouse button.

1
2
3
function mouseClicked() {
  console.log("The mouse button was clicked");
}

There are some more functions to find out if the user has moved the mouse, dragged the mouse (moving while it is pressed) or used the mouse wheel. You can look them up in the reference.

This post is licensed under CC BY 4.0 by the author.