App window restore on keypress

 ̄綄美尐妖づ 提交于 2020-01-03 03:20:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!