Chrome Native Client,使用 onKeyDown 事件时出错

发布于 2025-01-04 12:19:43 字数 1180 浏览 2 评论 0原文

我正在尝试运行一个非常基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2025-01-11 12:19:43

解决了。在页面的其他地方,包含游戏模块的 DIV 的内容发生了更改,从而从内存中删除了该模块。

Solved. Elsewhere on the page, the DIV that contains the game module was having its contents changed, which removed the module from memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文