问题
In my app I have added a functionality to minimize app window to system tray on keypress (on ESC or Pause/Break buttons press). So when pressing them the window get minimized.
Is there a way to add functionality to restore app window on certain keypress (even if other application will be currently active)?
For example I press Pause and the window is minimized. I press Pause and the app window is restored.
回答1:
Here is the solution extracted from node-webkit wiki :
// Load native UI library.
var gui = require('nw.gui');
var option = {
key: "Ctrl+Shift+A",
active: function() {
console.log("Global desktop keyboard shortcut: " + this.key + " active.");
},
failed: function(msg) {
// :(, fail to register the |key| or couldn't parse the |key|.
console.log(msg);
}
};
// Create a shortcut with |option|.
var shortcut = new gui.Shortcut(option);
// Register global desktop shortcut, which can work without focus.
gui.App.registerGlobalHotKey(shortcut);
// If register |shortcut| successfully and user struck "Ctrl+Shift+A", |shortcut|
// will get an "active" event.
// You can also add listener to shortcut's active and failed event.
shortcut.on('active', function() {
console.log("Global desktop keyboard shortcut: " + this.key + " active.");
});
shortcut.on('failed', function(msg) {
console.log(msg);
});
// Unregister the global desktop shortcut.
gui.App.unregisterGlobalHotKey(shortcut);
This example show you how to create a global shortcut listener and the different way to listen the event. This also show you how to unregister the shortcut.
来源:https://stackoverflow.com/questions/26715227/app-window-restore-on-keypress