【问题标题】:Node webkit hotkey example not working节点 webkit 热键示例不起作用
【发布时间】:2015-05-21 20:09:40
【问题描述】:

我正在尝试使用 node webkits 热键示例,可以在此处的快捷方式页面上查看该示例:https://github.com/nwjs/nw.js/wiki/Shortcut

这是我的代码:

test.js

// Load native UI library.
var gui = window.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);

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World!</title>
    <script>
        require("./test.js");
    </script>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node.js <script>document.write(process.version)</script>.
  </body>
</html>

package.json

{
  "name": "nw-demo",
  "main": "index.html",
    "dependencies": {
    "nw": "^0.12.0"
  },
  "scripts": {
    "start": "nw"
  }
}

它在 test.js 的这一行中断说 undefined 不是一个函数。

var shortcut = new gui.Shortcut(option);

【问题讨论】:

  • 该网站上的示例目前似乎已被破坏(至少在 OS X 中)。看到这个github.com/nwjs/nw.js/issues/3263
  • 是的,这就是我在 repo 上创建的问题。

标签: node.js node-webkit hotkeys


【解决方案1】:

只需删除这一行:

gui.App.unregisterGlobalHotKey(shortcut);

在您的代码中注册它,然后删除。它对我来说效果很好(mac,nwjs 0.12)

【讨论】:

  • 我已经试过了,还是不行。无论如何,它在到达那条线之前就中断了。
  • 未定义不是函数。
  • 在哪一行?在你的 html 中尝试&lt;script type="text/javascript" src="./test.js"&gt;&lt;/script&gt;
  • 正如我原来的帖子所说,它打破了这一行var shortcut = new gui.Shortcut(option);
  • 你如何运行你的应用程序?
【解决方案2】:

正如 (https://github.com/nwjs/nw.js/issues/3263#issuecomment-89679115) 中的 shaynem 指出的那样,您至少应该运行 node-webkit >= 0.10.0

您应该确保您的 PATH(或您正在使用的构建工具,例如 nodebob)在升级后没有引用任何旧的剩余版本的 node-webkit。

此外,如果您使用的是 Ubuntu,您可能会偶然发现一个未解决的问题 (https://github.com/nwjs/nw.js/issues/3199)

【讨论】:

    【解决方案3】:

    注意 webkit 0.13.0 有一个错误,即 k 是小写的,即使该方法是用大写的 K 记录的。试试:

    registerGlobalHotkey(shortcut);
    

    要验证此错误是否存在,请执行以下操作:

    var util = require('util');
    console.log(util.inspect(nw.App));
    

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2016-04-05
      • 2019-04-29
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      相关资源
      最近更新 更多