How do I On window close (x)

0 favourites
  • 8 posts
  • How do I fire event when X is clicked to close window?

  • keyboard On key X pressed --> Browser Close

    you mean to close the window, or maybe you meant fire an event after the window is closed?

  • When I click the X in the upper right of nw.js window, fire an event before closing. I can't find any way to do this.

  • Perhaps by creating a state triggered by pressing X condition. Once triggered, event is executed, once this event is executed, close window

  • You don't understand Fraktal.

    Ashley , there is no current way to check if window is closing in nw.js

    I want to save data to file before user clicks X to close window, but can't because the nw.js object has no condition for this.

    We need a condition to check if window is closing.

    This is important so that user doesn't lose their progress if they close out. At the moment if I X out of program, nothing can be saved. So a condition that checks is needed.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's a reference on how to do it with JavaScript:

    My next question that I'll have investigate later is if you can access nw.js with JavaScript with the browser plugin.

    If that's possible we can execute a few lines of js at the start of the game that will call a c2 function when the window closes.


    I'll assume for a moment that you can access nw.js with the browser object.

    You'll need the function and browser objects, then execute this js at the start of the layout:

    "var gui = require('nw.gui');

    var win = gui.Window.get();

    win.on('close', function() {

    c2_callFunction('nw.js close',[]);


    Then add this event anywhere. I should be called before the window is closed.

    On function "nw.js close"

    --- do something

    If the js causes an error so it doesn't work I'll have to look into it further when I get a chance.

  • thanks, i'll try that out!

  • Prominent

    Here's the working script for the latest nw.js:

    "var win=nw.Window.get(); win.on('close', function() {c2_callFunction('nw.js close',[]); win.close(true);});"

    I had forgot to make it actually close after the trigger. You can change 'nw.js close' to any function name you want to use.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)