Py5MouseEvent#

Datatype for providing information about mouse events.

Examples#

 1def setup():
 2    py5.size(200, 200, py5.P2D)
 3    py5.rect_mode(py5.CENTER)
 4
 5
 6def draw():
 7    py5.square(py5.random(py5.width), py5.random(py5.height), 10)
 8
 9
10def mouse_clicked(e):
11    modifiers = e.get_modifiers()
12    msgs = []
13    if modifiers & e.SHIFT:
14        msgs.append('shift is down')
15    if modifiers & e.CTRL:
16        msgs.append('control is down')
17    if modifiers & e.META:
18        msgs.append('meta is down')
19    if modifiers & e.ALT:
20        msgs.append('alt is down')
21    py5.println('mouse clicked: ' + (','.join(msgs) if msgs else 'no modifiers'))
 1def setup():
 2    py5.size(200, 200, py5.P2D)
 3    py5.rect_mode(py5.CENTER)
 4
 5
 6def draw():
 7    py5.square(py5.random(py5.width), py5.random(py5.height), 10)
 8
 9
10def mouse_clicked(e):
11    py5.println('mouse click count:', e.get_count())

Description#

Datatype for providing information about mouse events. An instance of this class will be passed to user-defined mouse event functions if py5 detects those functions accept 1 (positional) argument, as demonstrated in the example code. The mouse event functions can be any of mouse_clicked(), mouse_dragged(), mouse_moved(), mouse_entered(), mouse_exited(), mouse_pressed(), mouse_released(), or mouse_wheel(). Mouse events can be generated faster than the frame rate of the Sketch, making mouse event functions useful for capturing all of a user’s mouse activity.

Underlying Processing class: MouseEvent

The following methods and fields are provided:

  • get_action(): Return the mouse event’s action.

  • get_button(): Identify the mouse button used in the event.

  • get_count(): Get the number of mouse clicks.

  • get_millis(): Return the event’s timestamp.

  • get_modifiers(): Integer value used to identify which modifier keys (if any) are currently pressed.

  • get_native(): Retrieve native mouse event object.

  • get_x(): Return the x position of the mouse at the time of this mouse event.

  • get_y(): Return the y position of the mouse at the time of this mouse event.

  • is_alt_down(): Return boolean value reflecting if the Alt key is down.

  • is_control_down(): Return boolean value reflecting if the Control key is down.

  • is_meta_down(): Return boolean value reflecting if the Meta key is down.

  • is_shift_down(): Return boolean value reflecting if the Shift key is down.

Updated on April 27, 2022 10:44:51am UTC