# How to tell if the mouse is over the window?

**URL:** <https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285>\
**Category:** Processing\
**Created:** [November 7, 2018, 2:12am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285 "2018-11-07T02:12:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/angle/32/2026_2.png) [@Angle](https://discourse.processing.org/u/Angle)\
**Post date:** [November 7, 2018, 2:12am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/1 "2018-11-07T02:12:11Z")

</div>

I looked through the reference and I don’t see anything for that. Do I just need to use mouseEnter() and mouseLeave() events? 😕

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 7, 2018, 4:27am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/2 "2018-11-07T04:27:34Z")

</div>

yes, very good question,  
as this:

```auto
void setup() {
  size (200, 200);
}

int ccol = 0;

void draw() {
  if ( overmyWindow() ) { ccol = 200; } else { ccol = 0; }
  background(0, ccol, 0);
}

boolean overmyWindow() {
  println(" mouseX "+mouseX+" mouseY "+mouseY);
  if ( (mouseX > 2) && (mouseX <= width -2) && (mouseY > 2) && (mouseY <= height -2) ) return true;
  return false;
}

```

only works as long you move the mouse very slow over the  
window border.

But what is a **real bad thing** :  
it still works if the drawing window is NOT the activ one.

//\_\_ system\_info  
processing 3.4 JAVA mode  
windows 7

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 7, 2018, 6:33am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/3 "2018-11-07T06:33:12Z")

</div>

If you add “focused” to your overMyWindow if statement, then it should return only if the sketch is focused.

---

<div class="post-metadata">

**Author:** ![Angle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/angle/32/2026_2.png) [@Angle](https://discourse.processing.org/u/Angle)\
**Post date:** [November 8, 2018, 1:07am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/4 "2018-11-08T01:07:49Z")

</div>

…Isn’t there a better way though? Even if I have to dig into the underlying window code or whatever? Or do I just need to add a pause button? 😕

Edit: Hmm. I’m digging into the [javadoc](https://processing.github.io/processing-javadocs/core/), and I would expect either PApplet of PSurface to have his functionality, but it doesn’t look like either of them do.

---

<div class="post-metadata">

**Author:** ![Angle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/angle/32/2026_2.png) [@Angle](https://discourse.processing.org/u/Angle)\
**Post date:** [November 8, 2018, 1:54am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/5 "2018-11-08T01:54:38Z")

</div>

Alright, I added this functionality myself with:

```auto
public boolean mousePresent = true;
    
    public void mouseExited() {
    	mousePresent = false;
    }
    
    public void mouseEntered() {
    	mousePresent = true;
    }

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 8, 2018, 2:41am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/6 "2018-11-08T02:41:33Z")

</div>

cool, where you find this?

[https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html#mouseEntered(java.awt.event.MouseEvent)](https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html#mouseEntered(java.awt.event.MouseEvent))

i can now use:

```auto
  ccol = ( focused && mousePresent ) ? 255 : 0;
  background(0, ccol, 0);

```

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 8, 2018, 4:18pm UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/7 "2018-11-08T16:18:17Z")

</div>

I don‘t quite know what Would be better. I mean, you can get if the window is focused and if the mouse is inside the bounds… not much more needed to see if it‘s focused and within bounds 😅

---

<div class="post-metadata">

**Author:** ![Angle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/angle/32/2026_2.png) [@Angle](https://discourse.processing.org/u/Angle)\
**Post date:** [November 10, 2018, 1:33am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/8 "2018-11-10T01:33:46Z")

</div>

Well no, if the mouse goes outside the window, it leaves mouseX and mouseY at whatever they were before it left the window. And focused stays true until you click on something else. So if you check both of those, then move the mouse outside the window but don’t click on anything… It still registers as inside. Which, in combination with my panning code, means it will continue panning even after the mouse has left the window.

As for where I found my last code? I just made it up. It’s pretty standard though, you’ll see basically that all over the place.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 10, 2018, 8:33am UTC](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/9 "2018-11-10T08:33:48Z")

</div>

Oh Yeah, didn‘t think about focused not changing without clicking outside 😅 Anyway, good to know that mouseEntered is a thing 😊
