gpt4 book ai didi

javascript - 无法使用 Node js 访问桌面应用程序中的窗口关闭功能

转载 作者:IT老高 更新时间:2023-10-28 23:03:35 28 4
gpt4 key购买 nike

我正在使用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();
}
});
});

请查看随附的屏幕截图。我能够对"file"和“Windows”中的自定义添加功能执行操作。

但我不知道如何在标题栏中的默认应用程序关闭按钮被单击或通过右键单击 Windows 任务栏中的应用程序关闭时捕获事件。请帮忙。 enter image description here

提前致谢

最佳答案

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

你应该这样做:

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

我尝试了上述方法,效果很好。它可以同时捕获“关闭按钮”点击和 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

关于javascript - 无法使用 Node js 访问桌面应用程序中的窗口关闭功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32518508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com