home/ atoms/ processing-event-functions

Processing event functions like mousePressed() interrupt draw() cleanly, running once per event

Event functions (mousePressed(), mouseReleased(), mouseMoved(), mouseDragged(), keyPressed(), keyReleased()) are called by Processing outside the normal draw() loop. Unlike the mousePressed variable (which is true as long as the button is held), the function mousePressed() runs exactly once per button press. Events are queued and dispatched at the end of a draw() cycle, preventing interruption mid-frame. The function redraw() triggers one execution of draw() when a program is paused with noLoop(), enabling event-driven updates without continuous rendering.

Examples

void mousePressed() { redraw(); // update canvas once on click } void setup() { noLoop(); } // don’t draw continuously

Assessment

Explain the difference between checking mousePressed (variable) inside draw() and defining a mousePressed() function. Give a use case where each is more appropriate.

“Functions called events alter the normal flow of a program when an action such as a key press or mouse movement takes place. An event is a polite interruption to the normal flow of a program.”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 51