# Detecting when the mouse enters / exits sketch window

**URL:** <https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798>\
**Category:** Coding Questions\
**Created:** [November 21, 2018, 5:10pm UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798 "2018-11-21T17:10:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 21, 2018, 5:10pm UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/1 "2018-11-21T17:10:26Z")

</div>

I am interested in creating a new feature for G4P but I need to detect when the mouse enters and exits the client area (drawing area) of the sketch.

Since Processing does not generate these events I am trying to do this by adding my own mouse listeners using the code below. The problem is that I get very sporadic output for enter / exit events and only when I drag the sketch window. Also these events don’t relate to the mouse entering / exiting the sketch. The other events are not detected by my listener at all.

I have created equivalent code for P2D / P3D which works perfectly. I can post it here if it helps.

Any ideas will be welcome. 😁

Main Sketch code

```auto
import javax.swing.JFrame;

void setup() {
  size(200, 200);
  PointerListenerAWT l_AWT = new PointerListenerAWT();
  JFrame frame = (JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
  frame.addMouseListener(l_AWT);
  frame.addMouseMotionListener(l_AWT);
}

void draw() {
  background(0);
}

```

Java Tab : PointerListenerAWT.java

```auto
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class PointerListenerAWT implements MouseListener, MouseMotionListener {
  
  public void mouseDragged(MouseEvent e) {
    System.out.println("Drag @ " + System.currentTimeMillis());
  }

  public void mouseMoved(MouseEvent e) {
    System.out.println("Move @ " + System.currentTimeMillis());
  }

  public void mouseClicked(MouseEvent e) {
    System.out.println("Click @ " + System.currentTimeMillis());
  }

  public void mousePressed(MouseEvent e) {
    System.out.println("Press @ " + System.currentTimeMillis());
  }

  public void mouseReleased(MouseEvent e) {
    System.out.println("Release @ " + System.currentTimeMillis());
  }

  public void mouseEntered(MouseEvent e) {
    System.out.println("Enter @ " + System.currentTimeMillis());
  }

  public void mouseExited(MouseEvent e) {
    System.out.println("Exit @ " + System.currentTimeMillis());
  }
}

```

I using an iMac running OSX 10.13.6

BTW it is not possible to use mouseX and mouseY to detect when the mouse enters/exits because there values are constrained to the client area size.

---

<div class="post-metadata">

**Author:** ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)\
**Post date:** [November 21, 2018, 9:57pm UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/2 "2018-11-21T21:57:01Z")

</div>

`PApplet` has `mouseEntered()` and `mouseExited()` which you can override in the same way as `mousePressed()`.

If you are making a library, you can register method for `mouseEvent(MouseEvent e)` and you should get events with actions `MouseEvent.ENTER` and `MouseEvent.EXIT`.

(That is `processing.event.MouseEvent` we are talking about - should work for all renderers.)

---

<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 21, 2018, 11:17pm UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/3 "2018-11-21T23:17:19Z")

</div>

> [@How to tell if the mouse is over the window?](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/5):
>
> Alright, I added this functionality myself with: public boolean mousePresent = true; public void mouseExited() { mousePresent = false; } public void mouseEntered() { mousePresent = true; }

and

> [@How to tell if the mouse is over the window?](https://discourse.processing.org/t/how-to-tell-if-the-mouse-is-over-the-window/5285/3):
>
> If you add “focused” to your overMyWindow if statement, then it should return only if the sketch is focused.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 22, 2018, 8:56pm UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/4 "2018-11-22T20:56:38Z")

</div>

> [@Jakub](#):
>
> If you are making a library, you can register method for `mouseEvent(MouseEvent e)` and you should get events with actions `MouseEvent.ENTER` and `MouseEvent.EXIT` .

Do I feel like the village idiot 🙄 I don’t know how I missed that, G4P already registers for mouse events but does not use the ENTER /EXIT events.  
Thanks guys.

---

<div class="post-metadata">

**Author:** ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)\
**Post date:** [November 22, 2018, 11:17pm UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/5 "2018-11-22T23:17:55Z")

</div>

Haha, don’t worry. These are not in the reference and I also didn’t know about them until recently, when I was working on some mouse input stuff 🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 25, 2018, 1:15am UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/6 "2018-11-25T01:15:38Z")

</div>

It might actually be nice to mention / allude to in the reference! Reacting to mouse entering / exiting isn’t really an exotic thing to want to do.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [November 25, 2018, 9:05am UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/7 "2018-11-25T09:05:59Z")

</div>

> [@jeremydouglass](#):
>
> It might actually be nice to mention / allude to in the reference!

I have raised this as an [issue](https://github.com/processing/processing-docs/issues/720) on Github

---

<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 25, 2018, 9:34am UTC](https://discourse.processing.org/t/detecting-when-the-mouse-enters-exits-sketch-window/5798/8 "2018-11-25T09:34:40Z")

</div>

i not think it is just a documentation problem,  
do you notice the **IDE color** on ?internal? functions  
and this ?valid but external? JAVA functions  
( under valid i mean a working JAVA function what not need a import … )  
 ![snap-014](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a59df78b03c71d275490a7eef7885dc81d6bd425.png)
