Class Name
MouseEvent
Description
A MouseEvent object contains information about a mouse event that has occurred. This object is automatically passed to the mouse event functions (mousePressed(), mouseReleased(), mouseClicked(), mouseMoved(), mouseDragged(), and mouseWheel()) when they are called with an event parameter.
The MouseEvent object provides additional information beyond what's available in the global mouse variables (mouseX, mouseY, mouseButton, etc.). For most basic mouse interactions, using the global variables is sufficient and more convenient. However, the MouseEvent object is useful when you need more detailed information about the mouse event, such as the exact button that was pressed, modifier keys that were held down, or the scroll wheel count.
Note that event coordinates from getX() and getY() are not affected by the windowRatio() setting, unlike the global mouseX and mouseY variables.
Examples
void setup() { size(400, 400); background(220); } void draw() { // Draw instructions fill(0); textAlign(CENTER); text("Click and drag with different mouse buttons", width/2, 20); text("Hold Shift, Ctrl, or Alt while clicking", width/2, 40); } void mousePressed(MouseEvent event) { // Get mouse coordinates from event int x = event.getX(); int y = event.getY(); // Get which button was pressed int button = event.getButton(); // Check for modifier keys boolean shiftDown = event.isShiftDown(); boolean ctrlDown = event.isControlDown(); boolean altDown = event.isAltDown(); // Set color based on button and modifiers if (button == LEFT) { fill(255, 0, 0); // Red for left button } else if (button == RIGHT) { fill(0, 255, 0); // Green for right button } else if (button == CENTER) { fill(0, 0, 255); // Blue for center button } // Modify brightness based on modifier keys if (shiftDown) { fill(red(color(0)), green(color(0)), blue(color(0)), 100); } if (ctrlDown) { stroke(255); strokeWeight(3); } else { noStroke(); } // Draw circle at mouse position ellipse(x, y, altDown ? 50 : 20, altDown ? 50 : 20); // Print event information println("Mouse pressed at (" + x + ", " + y + ")"); println("Button: " + button + ", Shift: " + shiftDown + ", Ctrl: " + ctrlDown + ", Alt: " + altDown); }
void setup() { size(400, 400); background(220); } void draw() { fill(0); textAlign(CENTER); text("Use mouse wheel to zoom, move mouse to pan", width/2, 20); text("Wheel count and coordinates shown below", width/2, 40); } void mouseWheel(MouseEvent event) { // Get wheel count (positive = down/away, negative = up/toward) float wheelCount = event.getCount(); // Get mouse position when wheel was used int x = event.getX(); int y = event.getY(); // Check modifier keys boolean shiftDown = event.isShiftDown(); boolean ctrlDown = event.isControlDown(); // Draw feedback fill(0, 150); rect(x-30, y-10, 60, 20); fill(255); textAlign(CENTER); text(int(wheelCount), x, y+5); // Print detailed information println("Mouse wheel at (" + x + ", " + y + ")"); println("Wheel count: " + wheelCount); println("Shift: " + shiftDown + ", Ctrl: " + ctrlDown); // Different behavior based on modifiers if (shiftDown) { println("Horizontal scroll mode"); } else if (ctrlDown) { println("Precision scroll mode"); } else { println("Normal scroll mode"); } } void mouseMoved(MouseEvent event) { // Show current mouse coordinates from event fill(255); rect(10, height-40, 200, 30); fill(0); textAlign(LEFT); text("Mouse: (" + event.getX() + ", " + event.getY() + ")", 15, height-20); }
Methods
getX()
Returns the horizontal coordinate of the mouse eventgetY()
Returns the vertical coordinate of the mouse eventgetButton()
Returns which mouse button was pressed (LEFT, RIGHT, CENTER)getCount()
Returns the number of mouse wheel clicks (for mouseWheel events)isShiftDown()
Returns true if the Shift key was held down during the eventisControlDown()
Returns true if the Control key was held down during the eventisAltDown()
Returns true if the Alt key was held down during the eventisMetaDown()
Returns true if the Meta/Cmd key was held down during the event
Related

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.