Detecting when the mouse enters / exits sketch window

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. :grin:

Main Sketch code

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

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.

1 Like

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.)

5 Likes

and

1 Like

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

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 :slightly_smiling_face:

1 Like

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.

2 Likes

I have raised this as an issue on Github

2 Likes

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

1 Like