无法使用节点js访问桌面应用程序中的窗口closuresfunction

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

我的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(); } }); }); 

请find附件的截图。 我能够在“文件”和“Windows”中执行自定义添加function的操作。

但是我不知道如何捕捉事件,当标题栏中的默认应用程序closuresbutton被点击或closures,通过右键单击从Windows任务栏的应用程序。 请帮忙。 在这里输入图像描述

提前致谢

更新 (添加了更多的代码行来显示正确的事件触发方式)

你应该这样做:

 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 }); 

我试过以上,它的作品完美。 它捕获“closuresbutton”点击和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

最后,我通过隐藏默认标题栏并添加一个自定义标题栏来完成此操作。

为此,我在窗口创build时将appJs中的“showChrome”属性设置为false,默认为true。

代码更改是

  var window = app.createWindow({ width : 1024,// 640 height : 768, **showChrome: false**, icons : __dirname + '/content/icons' }); 

但是当'showChrome'属性设置为false时,任务栏也会隐藏,直到我们最小化或者还原应用程序。