Chrome Native Client,使用 onKeyDown 事件时出错
我正在尝试运行一个非常基本的 Chrome Native Client 应用程序。我想做的是响应击键,例如每当用户按下某个键时显示“您按下了 X”。我已经花了一整天的时间,但每次按下一个键,我都会收到“Uncaught TypeError: Object # has no method 'postMessage'”。
错误全部在 Javascript 中; Native Client C++ 模块工作正常。
文档头部中的Javascript:
myModule = null; // Global application object.
function moduleDidLoad() {
myModule = document.getElementById('mymodule');
alert("module loaded!") // this works
myModule.postMessage('hello'); // this works, and posts 'hello' to the module
// ERROR
document.body.onkeydown = function() {myModule.postMessage('hi');}
}
在页面中:
<div id="listener">
<script type="text/javascript">
var listener = document.getElementById('listener');
listener.addEventListener('load', moduleDidLoad, true);
</script>
<embed name="nacl_module"
id="mymodule"
width=0 height=0
src="mymodule.nmf"
type="application/x-nacl" />
</div>
我尝试了大约15种不同的方法:通过使用addEventListener将其添加到body标签,通过使用onKeyDown将其直接添加到body标签......没有任何效果。
我有 C/C++ 和 PHP 经验,但我的 Javascript 确实很弱。我想我一定错过了一些基本且明显的东西。
I'm trying to get a very basic Chrome Native Client application running. What I'd like to do is respond to keystrokes, for example by displaying "You pressed X" whenever a user presses a key. I've been at it all day, but every time I press a key, I get "Uncaught TypeError: Object # has no method 'postMessage'".
The errors are all in the Javascript; the Native Client C++ module works fine.
Javascript in head of document:
myModule = null; // Global application object.
function moduleDidLoad() {
myModule = document.getElementById('mymodule');
alert("module loaded!") // this works
myModule.postMessage('hello'); // this works, and posts 'hello' to the module
// ERROR
document.body.onkeydown = function() {myModule.postMessage('hi');}
}
In page:
<div id="listener">
<script type="text/javascript">
var listener = document.getElementById('listener');
listener.addEventListener('load', moduleDidLoad, true);
</script>
<embed name="nacl_module"
id="mymodule"
width=0 height=0
src="mymodule.nmf"
type="application/x-nacl" />
</div>
I've tried it about 15 different ways: by adding it to the body tag with addEventListener, by adding it directly to the body tag with onKeyDown... nothing works.
I have experience with C/C++ and PHP but my Javascript is really weak. I think I must be missing something fundamental and obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。在页面的其他地方,包含游戏模块的 DIV 的内容发生了更改,从而从内存中删除了该模块。
Solved. Elsewhere on the page, the DIV that contains the game module was having its contents changed, which removed the module from memory.