node.js - Node webkit 热键示例不起作用

标签 node.js node-webkit hotkeys

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

这是我的代码:

测试.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>

包.json

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

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

var shortcut = new gui.Shortcut(option);

最佳答案

只需删除这一行:

gui.App.unregisterGlobalHotKey(shortcut);

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

关于node.js - Node webkit 热键示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29132269/

相关文章:

java - 如何自动完成方法名称

java - 我可以在Eclipse中定义自定义键盘快捷键吗?

node.js - 在 EJS 中使用 console.log

Node-Webkit - 启动最大化

javascript - Node.js 不能要求同一目录中的 .js 文件

node-webkit - 检查新窗口是否仍然打开

javascript - js - 设置上传文件的热键

node.js - 如何将 STDIN 传递给 node.js 子进程

javascript - 修改 strapi 中的响应对象

node.js - 登录后 keystonejs 重定向无法使用函数作为第二个参数