One of those should handle the Alt-F4 Event, but i donāt know how to get the precise one. Iāll look into it a bit more and tell you when i find something
Ugh, ok i tried to find something, but apparently the only way to remove one is to have the specific Event as a WindowListener already, but i donāt know how to identify the right one. So you might want to just override the existing one by adding a new one with the addWindowListener() function, but iām not sure if thatās goodā¦
Yeah, I am not sure if this will help you in your case.
An idea would be to separate your program from the sketch and keep track of your programās state. The main P3 sketch could be run as an invisible sketch which launches a second sketch and it is there where you manage your program. If the user exits and the prompt declines the exit step, you could close the second āchildā sketch and open a new one loading the current state of your program.
I hope there is a better way to do this though. That is my suggestion and probably not applicable if you are not managing states.
You will also need to keep in mind that if the OS kills your program, you might not have an option but to exit without this flow. Just thinking out loudā¦
What happens when you press Alt-F4 is that your application is asked to close, but itās not forces. Only the WindowListeners Closing event is called.
What exactly happens is left to you, but that āyouā in this case is the way that processing handles this case, so overriding it might not be a good ideaā¦ on the other Hand, you can probably see how probably handles this and if it only calls dispose() or so, then that would be perfect, but i donāt know thatā¦
Found it! This is what Processing does internally :
//sketch is a field of PSurfaceNone of class PApplet
//frame is a Java.awt.Frame field in PSurfaceAWT
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
sketch.exit(); // don't quit, need to just shut everything down (0133)
}
});
So you can just use this :
PSurfaceAWT frame = (processing.awt.PSurfaceAWT.SmoothCanvas)getSurface().getNative()).getFrame();
PApplet sketch = this;
void setup() {
frame.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//put your Code here
if (readyToClose())
sketch.exit();
}
}
}
boolean readyToClose() {
if (allSaved) {
return true;
} else {
return false;
}
}