We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Mouse Click in certain areas of screen only
Page Index Toggle Pages: 1
Mouse Click in certain areas of screen only? (Read 2322 times)
Mouse Click in certain areas of screen only?
Apr 2nd, 2009, 1:35am
 
Hey guys

I am looking to only run somthing when the mouse is clicked within a certain shape on the screen. If the mouse is outside the shape i dont want anything to happen.

How do i specify this in processing, or what methods would be used? I am unsure of how i can tell the program to only react in certain areas.

Thanks
Re: Mouse Click in certain areas of screen only?
Reply #1 - Apr 2nd, 2009, 1:58am
 
Well you can find the mouse position with mouseX and mouseY, so you can check in your code where the mouse is when it's been clicked, and then either take action or not depending, e.g.

Code:
void mousePressed()
{
if(mouseX >40 && mouseX<80 && mouseY>40 && mouseY<100)
{
//do something.
}
}
Re: Mouse Click in certain areas of screen only?
Reply #2 - Apr 2nd, 2009, 2:02am
 
This recent thread: Area detection HELP is related: if you can find out that a point (the mouse click one) is within a shape, then you can do your action, otherwise you just ignore it.
Re: Mouse Click in certain areas of screen only?
Reply #3 - Apr 2nd, 2009, 4:21am
 
Quote:
Well you can find the mouse position with mouseX and mouseY, so you can check in your code where the mouse is when it's been clicked, and then either take action or not depending, e.g.

Code:
void mousePressed()
{
 if(mouseX >40 && mouseX<80 && mouseY>40 && mouseY<100)
 {
   //do something.
 }
}



Ah yes, so for a square i can just specify the 4 sides co ords in the if test and do a test that way. I was thinking about the mouseX and mouseY but did not have a clear example of how it operated.

Also thank you for the area thread, that was also useful as it gave me an example of a program using just mousex and mousey. Cheers guys.
Re: Mouse Click in certain areas of screen only?
Reply #4 - Apr 2nd, 2009, 11:44am
 
for a circle, where : ellipse(x,y, h,w);

mouseReleased() {
  if (dist(mouseX,mouseY, x,y) < h/2) {
     //  do something
  }
}
Page Index Toggle Pages: 1