mouse_button#

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.

Examples#

 1# click within the image and press
 2# the left and right mouse buttons to
 3# change the value of the rectangle
 4def draw():
 5    if py5.is_mouse_pressed and py5.mouse_button == py5.LEFT:
 6        py5.fill(0)
 7    elif py5.is_mouse_pressed and py5.mouse_button == py5.RIGHT:
 8        py5.fill(255)
 9    else:
10        py5.fill(126)
11
12    py5.rect(25, 25, 50, 50)
 1# click within the image and press
 2# the left and right mouse buttons to
 3# change the value of the rectangle
 4def draw():
 5    py5.rect(25, 25, 50, 50)
 6
 7
 8def mouse_pressed():
 9    if py5.mouse_button == py5.LEFT:
10        py5.fill(0)
11    elif py5.mouse_button == py5.RIGHT:
12        py5.fill(255)
13    else:
14        py5.fill(126)

Description#

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.)

Underlying Processing field: mouseButton

Updated on November 12, 2021 11:30:58am UTC