【发布时间】: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