How to block windows desktop appliaction exit / quit?

This problem have been mentioned in the old community, I am here to ask again, for this is very important for desktop application.

Thank you for your answer

Do you want to receive an event when this occurs, or do you want to prevent the “X” button from working?

I want to receive a event, and can let me decide to closing the application or not. Like this:

public function onClosing( e:Event ):Void{
//some code to decide whether close the application or not.
var canClose = false;
if( canClose ){
}else{
e.preventDefault();
}
}

On native targets, SDL dispatches an “SDL_QUIT” event, which I suppose could be discarded, possibly. It might also lead to instability. I don’t think we can discern the difference between causes for a quit event, but someone could experiment with this, would be happy to give pointers to what one would change in the source to try.

Is this suppose to work on openfl-next?

stage.addEventListener(openfl.events.Event.CLOSE, OnClose);

Lime has quit events now, on the OpenFL layer, you should handle stage deactivate events (probably) but it is possible now to hook into Lime close events :slight_smile:

An example of code please? :smile:

Oh, something like this:

stage.addEventListener (Event.DEACTIVATE, stage_onDeactivate);

...

private function stage_onDeactivate (event:Event):Void {
    
    Actuate.pauseAll ();
    
    // perform shutdown logic
    
}

There is an ACTIVATE event as well you can listen to.

This triggers when the application is minimized, closed, or sent to the background on mobile. This is usually what you want, because you need to handle pausing game logic automatically, as well as saving in case the application is not resumed (in the case of mobile).

OpenFL uses Lime shutdown logic internally to handle destroying OpenAL, or flushing SharedObjects automatically. Most things can be done in ACTIVATE and DEACTIVATE instead and should be fine :slight_smile:

Is it openfl.events.Event.ACTIVATE and openfl.events.Event.DEACTIVATE?

[...] lines 193-195 : Module openfl.events.Event does not define type ACTIVATE

What am I doing wrong that I got this error for both ACTIVATE and DEACTIVATE, or is it some bug (OpenFL 3.4.0, lime 2.7.0)?

What does your exact code look like?

Like this:

stage.addEventListener(openfl.events.Event.ACTIVATE, OnActivate);
stage.addEventListener(openfl.events.Event.DEACTIVATE, OnDeactivate);

[…]

public function OnActivate(e:openfl.events.Event.ACTIVATE) {
    trace('activate');
}

public function OnDeactivate(e:openfl.events.Event.DEACTIVATE) {
    trace('deactivate');
}

Btw. why the error happens on function definitions, not on the line with stage.addEventListener(...) ?

Oh, got it, try this:

main.stage.addEventListener(openfl.events.Event.ACTIVATE, OnActivate);
main.stage.addEventListener(openfl.events.Event.DEACTIVATE, OnDeactivate);

...

public function OnActivate(e:openfl.events.Event) {
    trace('activate');
}

public function OnDeactivate(e:openfl.events.Event) {
    trace('deactivate');
}

(or import openfl.events.Event at the top, and just use Event in the code)

Event is a class type, ACTIVATE is a static String value, addEventListener uses string names for events, while the listeners use an event instance as the argument

Ohh indeed my mistake, thx for your time…
Too much coffeine i guess :blush:

No worries! :slight_smile:

Let me know how it works :slight_smile:

On Linux and neko I think it works as expected: activate and deactivate triggers on starting/closing and minimising/restoring but don’t trigger on focus lost/restore.

Bah! It doesn’t trigger when the system closes the app (like killing or even closing window with [X]). So is it expected behavior?

No, I think the goal was for this to always trigger on clean exit as well, so I’d call that a bug :frowning:

Which OS?

Linux/Ubuntu (targeting both neko and native).