无法使用节点js访问Desktop Application中的窗口关闭功能

Akh*_*esh 15 javascript jquery desktop-application node.js backbone.js

我正在使用node.js和backbone.js开发一个Windows桌面应用程序.我想在用户通过单击标题栏上的关闭按钮或右键单击Windows任务栏中的应用程序来关闭应用程序时执行操作.

我的app.js看起来像这样,

 var app = module.exports = require('appjs');


app.serveFilesFrom(__dirname + '/content/assets/');

var menubar = app.createMenu([ {
label : '&File',
submenu : [ {
    label : 'E&xit',
    action : function() {
        window.close();
    }
},{
    label : 'New',
    action : function() {
        window.test();
    }
} ]
}, {
label : '&Window',
submenu : [ {
    label : 'Fullscreen',
    action : function(item) {
        window.frame.fullscreen();
        console.log(item.label + " called.");
    }
}, {
    label : 'Minimize',
    action : function() {
            console.log("df");
        window.frame.minimize();
    }
}, {
    label : 'Maximize',
    action : function() {
        console.log("nnnnnnlaaaaaaaaaaaaaaa");
        window.frame.maximize();
    }
}, {
    label : ''// separator
}, {
    label : 'Restore',
    action : function() {
        window.frame.restore();
    }
} ]
} ]);

menubar.on('select', function(item) {
console.log("menu item " + item.label + " clicked");
});

var trayMenu = app.createMenu([ {
label : 'Show',
action : function() {
    window.frame.show();
},
}, {
label : 'Minimize',
action : function() {
    window.frame.hide();
}
}, {
label : 'Exit',
action : function() {
    window.close();
}
} ]);

var statusIcon = app.createStatusIcon({
icon : './data/content/icons/32.png',
tooltip : 'AppJS Hello World',
menu : trayMenu
});



 var window = app.createWindow({
 width : 1024,// 640
height : 768,
showChrome: true,
icons : __dirname + '/content/icons'
});
window.on('create', function() {
console.log("Window Created");
window.frame.show();
window.frame.center();
window.frame.maximize();
window.frame.setMenuBar(menubar);
});

window.on('ready', function() {
console.log("Window Ready");    
window.require = require;
window.process = process;
window.module = module;
//window.frame.openDevTools();
window.fileAssoc = process.mainModule.filename;
//window.readMyFile();
function F12(e) {
    return e.keyIdentifier === 'F12'
}
function Command_Option_J(e) {
    return e.keyCode === 74 && e.metaKey && e.altKey
}


        });*/


window.addEventListener('keydown', function(e) {
console.log("hi");      
    if (F12(e) || Command_Option_J(e)) {            
        window.frame.openDevTools();
    }
   });
   }); 
Run Code Online (Sandbox Code Playgroud)

请找到附带的屏幕截图.我能够对"文件"和"Windows"中的自定义添加功能执行操作.

但是,如果通过右键单击Windows任务栏中的应用程序单击或关闭标题栏中的默认应用程序关闭按钮,我不知道如何捕获事件.请帮忙. 在此输入图像描述

提前致谢

LGS*_*Son 5

更新(添加了一些代码行以显示事件触发的正确方法)

你应该这样做:

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

var win_main = gui.Window.get();
win_main.on('close', function () {
    this.hide(); // Pretend to be closed already
    alert("Closing...");

    // here you detect if data is saved, and if not, ask user if they want to save

    this.close(true);   // if this line executes the app closes, if not,
                        // app stays opened
});
Run Code Online (Sandbox Code Playgroud)

我尝试了上面的方法,它完美无缺.它捕获"关闭按钮"单击和Windows中的"Ctrl + F4"等快捷键.

进一步阅读(来自评论):

http://tutorialzine.com/2015/01/your-first-node-webkit-app/

https://nodesource.com/blog/node-desktop-applications

https://gist.github.com/LeCoupa/80eca2716a2b13c37cce

https://github.com/nwjs/nw.js/

https://github.com/nwjs/nw.js/wiki/window