Reputation: 1377
I have a program I am writing in processing and I need to be able to detect when the mouse has left the window/canvas. However, as far as I can tell, processing does not have anything similar to the mouseOut event. Is there any way to accomplish this using a callback or event or something of that nature?
Upvotes: 2
Views: 2000
Reputation: 1082
Using knowledge of the window position plus its borders you could also calculate if pointer is inside frame or not. So then you can call your function.
import java.awt.Point;
import java.awt.MouseInfo;
java.awt.Insets insets;
Point mouse, win;
void setup() {
size(400, 400);
frame.pack();
smooth();
}
void draw() {
setFrame();
if(insideFrame()) {
background(95);
}
else {
background(0);
}
}
//set position of frame
void setFrame()
{
mouse = MouseInfo.getPointerInfo().getLocation();
win = frame.getLocation();
if(!frame.isUndecorated()){
//add borders of window
insets = frame.getInsets();
win.x += insets.left;
win.y += insets.top;
}
}
boolean insideFrame() {
boolean in = false;
if(mouse.x-win.x >= 0 && width >= mouse.x-win.x)
if(mouse.y-win.y >= 0 && height >= mouse.y-win.y)
in = true;
return in;
}
Upvotes: 3
Reputation: 3411
Take a look at Java's MouseAdapter
class. Example code:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
void setup(){
frame.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
print("notify");
}
});
}
void draw(){}
Upvotes: 2