我正在尝试使用 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/