Thursday 26 March 2009

First 500 words (draft)

Mouse Events

Mouse events are the detection of mouse button presses and releases and mouse movement. They are called events because the program does not wait for the mouse to be pressed but, instead, an event handler is used that detects when the mouse has been pressed – this is called an event. Processing provides a simple way of detecting mouse events by providing a set of variables and functions that can be used to perform actions when a mouse event is detected.

Mouse Events in Processing

Processing provides event handlers for mouse presses, releases, movements and dragging. The simplest mouse event that Processing caters for is when a mouse button is pressed. This event detects any mouse button press – that is a click from any mouse button. A mouse press is defined as when the mouse button is pressed, not released.

To enable you to use these events, Processing provides both a mousePressed variable, and a mousePressed() function. We will describe the use of both of these before explaining why both exist

MousePressed

The mousePressed variable is a boolean that has the value true if a mouse press event is currently detected, or the value false if there is no mouse press event detected. This means it can be used in the draw() function to determine if a mouse button is currently pressed. Each time the draw() function is run, the boolean will be evaluated, which will have the value true if the mouse button is still pressed. This means that keeping the mouse button pressed will continue to run the corresponding code each time the draw() function is called.

An example of this shown in the example below. Each time the draw method is called, the mousePressed boolean variable is evaluated. When the program is started, the background is filled with black. If the mouse is pressed, the background will be filled with white.

void draw() {
// if the mouse is pressed
if (mousePressed) {
// white background
background(255);
// else the mouse is not pressed
} else {
// black background
background(0);
}
}

You will see that, if the mouse button is held down, the background remains white until the mouse button is released – at that point the background reverts to black. It might seem that many mouse events are being generated if the mouse button is held. However a single event is detected when the button is pressed, which sets the mousePressed variable to true. It remains true until the mouse button is released, when it reverts to being false.

MousePressed()

In addition to the mousePressed variable, Processing provides the mousePressed() function. This function can be defined in your program and is called whenever a mouse press event is detected. An example of this is shown below

void draw() {
// black background
background(0);
}

void mousePressed() {
background(255);
}

This time you will see a slight change to how the previous example worked. Every time a mouse button is pressed, the screen flashes white, then back to black. If the mouse button is held, the period of time the screen is white does not change – it still flashes white then black. This is because the mousePressed() function operates in a different way to the mousePressed variable.

Mouse Event Variables

MouseButton

mouseX and mouseY

pmouseX and pmouseY

Other Mouse Event Methods

Mouse Release

Mouse Dragging

Mouse Moving

Putting It All Together

No comments: