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