2015-03-18 12 views
3

我试图玩弄节点节点WebKit的热键例如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

网站上的例子似乎在此刻(至少在OS X)被打破。看到这个https://github.com/nwjs/nw.js/issues/3263 – dwkns 2015-04-13 11:42:08

+0

是的,这是我在回购上创建的问题。 – 2015-04-14 00:54:38

回答

0

只是删除这一行:

gui.App.unregisterGlobalHotKey(shortcut); 

在你的代码进行注册,然后删除。它为我工作好(MAC,nwjs 0.12)

+0

我已经试过了,它没有工作。无论如何,它在到达那条线之前都会中断。 – 2015-03-19 05:46:05

+0

这是什么意思? – 2015-03-19 05:56:22

+0

未定义不是函数。 – 2015-03-19 07:35:21

0

音符的WebKit 0.13.0具有这样即使该方法被证明以大写k为第k是小写的错误。尝试:

registerGlobalHotkey(shortcut); 

来验证这个bug存在做:

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