# That's strange! Detect mouse inside a box?

**URL:** <https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443>\
**Category:** Coding Questions\
**Created:** [August 23, 2020, 2:40pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443 "2020-08-23T14:40:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![HumanoidX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humanoidx/32/8506_2.png) [@HumanoidX](https://discourse.processing.org/u/HumanoidX)\
**Post date:** [August 23, 2020, 2:40pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/1 "2020-08-23T14:40:58Z")

</div>

I remember that I did that in a Periodic Table program before, my Dad accidentally deleted the D: drive in my computer and everything inside got lost.

This is my code:

```auto
boolean mouseIsBetween(float x1,float y1,float x2,float y2){
  if(mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2){
    return true;
  }else{
    return false;
  }
}

```

It must detect if the mouse is between x1,y2 to x2,y2, but it do not return true.

Sorry if there is any grammar mistake.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 23, 2020, 3:19pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/2 "2020-08-23T15:19:19Z")

</div>

the function looks ok

maybe you call the function not correctly

remember that the values you have to pass to the function are NOT the same as with rect(), because with

- `mouseIsBetween` you want absolute positions x,y and x,y
- with rect you have x,y and the sizeX,sizeY

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 23, 2020, 3:27pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/3 "2020-08-23T15:27:09Z")

</div>

working example with your function

```auto

boolean boolEnteringText=true; // entering text yes/no
String export=""; // current text input
String result = ""; // list of text on the right  
boolean showBlinkingLine=true; // show blinking cursor yes/no

void setup() {
  size(800, 800);
  background(0);
}

void draw() {
  background(0);
  text(export + blinkingLine(), 22, 22);
  text(result, width-112, 22);

  noFill();
  stroke(220); 
  rect(210, 4, 61, 27);
  fill(255);
  text("submit", 222, 22);
}

//---------------------------------------------------------------------------

void keyPressed() {
  if (boolEnteringText == true && key==BACKSPACE) {
    // delete last char
    if (export.length()>0) {
      export=export.substring(0, export.length()-1);
    }
  } else if (boolEnteringText == true && (key==RETURN||key==ENTER)) {
    // submit
    submit();
  }//else if
  else if (boolEnteringText == true && key != CODED) {
    // add char 
    export += key;
  }
}

void mousePressed() {
  if (mouseIsBetween(210, 4, 280, 4+27)) {
    submit();
  }
}

boolean mouseIsBetween(float x1, float y1, float x2, float y2) {
  if (mouseX >= x1 && mouseX <= x2 && mouseY >= y1 && mouseY <= y2) {
    return true;
  } else {
    return false;
  }
}

String blinkingLine() {
  // toggle showBlinkingLine
  if (frameCount%17==0) 
    showBlinkingLine = ! showBlinkingLine;

  // return line or nothing     
  if (showBlinkingLine) 
    return "|";
  else return "";
}

void submit() {
  // submit
  result+=export+"\n";
  export="";
  showBlinkingLine=true;
}
//

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 23, 2020, 3:45pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/4 "2020-08-23T15:45:55Z")

</div>

> **[Collision Detection](http://jeffreythompson.org/collision-detection/point-rect.php)**
>
> An online book about collision detection using Processing.

  
[Studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr)  
[Studio.ProcessingTogether.com/sp/pad/export/ro.9vAkI4JDuLs8s](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9vAkI4JDuLs8s)

---

<div class="post-metadata">

**Author:** ![HumanoidX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humanoidx/32/8506_2.png) [@HumanoidX](https://discourse.processing.org/u/HumanoidX)\
**Post date:** [August 24, 2020, 12:10pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/5 "2020-08-24T12:10:24Z")

</div>

Tested everything, don’t work… 😕

This is my code:  
[https://cutt.ly/cfpBITG](https://cutt.ly/cfpBITG)

Can you guys help me change it?

Thanks for the helps…

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 24, 2020, 2:54pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/6 "2020-08-24T14:54:41Z")

</div>

Hi @HumanoidX,

I took your example and applied a simple case when you press the bin image. You can reply the code to the other images.  
I noticed that the FunBlackboard.pde was missing the draw() function.  
Your code for the mouseIsBetween() method is correct assuming that you pass the correct coordinates.

I would recommend you to also look at the following example for better understanding 🙂 ([https://processing.org/examples/button.html](https://processing.org/examples/button.html))

```auto
import processing.sound.*;

float oldx;
float oldy;
float newx;
float newy;
boolean drewCurve = false;

PImage bin;
PImage style;
PImage rgb;
PImage a;

boolean binClicked = false;

void setup(){
  size(900,440);
  background(0);
  
  bin = loadImage("bin.png");
  rgb = loadImage("rgb.png");
  a = loadImage("a.png");
  style = loadImage("style.png");
  //drawPalette();
}

void draw(){
  background(0); 
  drawPalette();
//If you have clicked the bin a text message appears. If you click again the text message will dissapear
  if(binClicked){
    fill(255);
    text("clicked the bin",width/2, height/2);
  }

}

void mousePressed(){ 
//The bin is located between corrdinates (0,0) and (40,40)
  if(mouseX >= 0 && mouseX <= 40 && mouseY >= 0 && mouseY <= 40){
    binClicked = !binClicked; //alter the boolean state
  }
}

```

Hope it helps!

---

<div class="post-metadata">

**Author:** ![HumanoidX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humanoidx/32/8506_2.png) [@HumanoidX](https://discourse.processing.org/u/HumanoidX)\
**Post date:** [August 25, 2020, 12:58pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/7 "2020-08-25T12:58:52Z")

</div>

Thanks Miguel, It worked. 🥰 Looks like I can’t use the return function to output data. But it worked so I will not care about it. 😛

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 25, 2020, 1:05pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/8 "2020-08-25T13:05:29Z")

</div>

Hi @HumanoidX

Glad it worked  
But you can use the mouseIsBetween function still.  
Replace

```auto
void mousePressed(){ 
//The bin is located between corrdinates (0,0) and (40,40)
  if(mouseX >= 0 && mouseX <= 40 && mouseY >= 0 && mouseY <= 40){
    binClicked = !binClicked; //alter the boolean state
  }
}

```

by

```auto
void mousePressed(){ 
//The bin is located between corrdinates (0,0) and (40,40)
 if(mouseIsBetween(0,0,40,40)){
    binClicked = !binClicked; //alter the boolean state
  }
}

```

---

<div class="post-metadata">

**Author:** ![HumanoidX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humanoidx/32/8506_2.png) [@HumanoidX](https://discourse.processing.org/u/HumanoidX)\
**Post date:** [August 25, 2020, 1:27pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/9 "2020-08-25T13:27:56Z")

</div>

Guess what, don’t work anymore when I switch to the boolean…return code.

I have to use the very very loooooong code after all. 🤯

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 25, 2020, 1:30pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/10 "2020-08-25T13:30:59Z")

</div>

When using your code and substituting for what I have posted it worked for me.

Make sure that you are calling the functions in the proper place. The mousePressed() method is an event that detects when the mouse is being pressed. Only there I check if the bin was clicked.

---

<div class="post-metadata">

**Author:** ![HumanoidX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humanoidx/32/8506_2.png) [@HumanoidX](https://discourse.processing.org/u/HumanoidX)\
**Post date:** [August 28, 2020, 12:41pm UTC](https://discourse.processing.org/t/thats-strange-detect-mouse-inside-a-box/23443/12 "2020-08-28T12:41:34Z")

</div>

Never mind, it somehow worked again. 😕  
But it worked anyway… 😄
