我正在尝试使用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);
答案 0 :(得分:0)
只需删除此行:
gui.App.unregisterGlobalHotKey(shortcut);
在您的代码中注册,然后删除。它适用于我(mac,nwjs 0.12)
答案 1 :(得分:0)
正如(https://github.com/nwjs/nw.js/issues/3263#issuecomment-89679115)中的shaynem指出你应该至少运行node-webkit&gt; = 0.10.0
在升级后,您应该确保您的PATH(或您正在使用的构建工具,例如nodebob)没有引用任何旧的node-webkit剩余版本。
此外,如果您使用Ubuntu,您可能偶然发现一个未解决的问题(https://github.com/nwjs/nw.js/issues/3199)
答案 2 :(得分:0)
注意webkit 0.13.0有一个错误,其中k是小写的,即使该方法记录为大写K.尝试:
registerGlobalHotkey(shortcut);
验证此错误存在吗:
var util = require('util');
console.log(util.inspect(nw.App));