When a mouse button is pressed, the value of the system variable mouse_button is set to either LEFT, RIGHT, or CENTER, depending on which button is pressed.
# click within the image and press# the left and right mouse buttons to# change the value of the rectangledefdraw():ifpy5.is_mouse_pressedandpy5.mouse_button==py5.LEFT:py5.fill(0)elifpy5.is_mouse_pressedandpy5.mouse_button==py5.RIGHT:py5.fill(255)else:py5.fill(126)py5.rect(25,25,50,50)
# click within the image and press# the left and right mouse buttons to# change the value of the rectangledefdraw():py5.rect(25,25,50,50)defmouse_pressed():ifpy5.mouse_button==py5.LEFT:py5.fill(0)elifpy5.mouse_button==py5.RIGHT:py5.fill(255)else:py5.fill(126)
When a mouse button is pressed, the value of the system variable mouse_button is set to either LEFT, RIGHT, or CENTER, depending on which button is pressed. (If no button is pressed, mouse_button may be reset to 0. For that reason, it’s best to use mouse_pressed first to test if any button is being pressed, and only then test the value of mouse_button, as shown in the examples.)