nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.38k stars 3.88k forks source link

Error: Unable to register the hotkey #6228

Open korner-brazers opened 7 years ago

korner-brazers commented 7 years ago

All greetings) encountered such a mistake.

I copy the code from the example

<!DOCTYPE HTML>
<head>
    <title></title>

    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<script>
    var option = {
        key : "Ctrl+S",
        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 nw.Shortcut(option);

    // Register global desktop shortcut, which can work without focus.
    nw.App.registerGlobalHotKey(shortcut);

</script>

<a onclick="window.location.reload()">Reload</a>

</body>
</html>

At the first start of the program, everything works fine, but when the program reboots I get here such an error.

Error: Unable to register the hotkey
    at Function.Shortcut.registerGlobalHotKey (extensions::nw.Shortcut:204)
    at Object.bindingsAPI.compiledApi.registerGlobalHotKey (extensions::nw.App:117)
    at index.html:26

The error appears after the restart of the window, to reboot I use the function window.location.reload()

The event (active) does not work any more, but the keys remain registered because for example (Sublime Text) when pressing the (Ctrl+S) does not react until I close nw.exe

NWJS Version : SDK 0.26.0 Operating System : Windows 7

Christywl commented 7 years ago

I can reproduce this issue on Linux/Windows with nwjs-sdk-v0.26.0.

hal-ullr commented 6 years ago

I am experiencing this issue

It is simple enough to unregister the hotkey before reloading the page, but as far as I can tell there is no way to unregister the hotkey before the page reloads when the ‘Reload app’ context menu option is clicked which has the effect of breaking hotkeys until the app is actually closed and opened again

marcobraghim commented 6 years ago

I'm experimenting this issue right now. In my case I need to unregister hotkey when window loses focus (I suppose it should be default, but it dont)

Follow my code:

  var shortcut = new gui.Shortcut({
    key: 'Ctrl+n',
    active: function() {
      // Some action
    },
    failed: function(msg) { console.error(msg) }
  })

  gui.Window.get().on('focus', function() {
    gui.App.registerGlobalHotKey(shortcut)
  })

  gui.Window.get().on('blur', function() {
      gui.App.unregisterGlobalHotKey(shortcut)
  });

Error message is: Error: Unable to unregister the hotkey