Saturday, June 8, 2013

Processing code sample: detect mouse event

To detect mouse event implement mouse related callback function; mouseClicked(), mouseDragged(), mouseMoved(), mousePressed() and mouseReleased(). The system variable mouseX and mouseY contain the current X and Y coordinate of the mouse.

void setup(){
  size(400, 300);
  background(255);
}

void draw(){
}

// The mouseClicked() function is called once after a 
// mouse button has been pressed and then released.
void mouseClicked(){
  fill(0xFF, 0x00, 0x00);
  ellipse(mouseX, mouseY, 50, 50);
}

// The mouseDragged() function is called once every 
// time the mouse moves and a mouse button is pressed.
void mouseDragged(){
  fill(0x00, 0xFF, 0x00);
  ellipse(mouseX, mouseY, 50, 50);
}

// The mouseMoved() function is called every time the 
// mouse moves and a mouse button is not pressed.
void mouseMoved(){
  fill(0xFF, 0xFF, 0xFF);
  ellipse(mouseX, mouseY, 50, 50);
}

// The mousePressed() function is called once after 
// every time a mouse button is pressed. The mouseButton 
// variable (see the related reference entry) can be used 
// to determine which button has been pressed.
void mousePressed(){
  fill(0xFF, 0x00, 0xFF);
  rect(mouseX, mouseY, 50, 50);
}

// The mouseReleased() function is called every time a 
// mouse button is released.
void mouseReleased(){
  fill(0x00, 0xFF, 0xFF);
  rect(mouseX, mouseY, 50, 50);
}

detect mouse event in Processing
detect mouse event in Processing


No comments:

Post a Comment