Chrome Native Client,使用onKeyDown事件时出错 [英] Chrome Native Client, errors using onKeyDown event

查看:94
本文介绍了Chrome Native Client,使用onKeyDown事件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个非常基本的Chrome Native Client应用程序.我想做的是响应击键,例如,只要用户按下某个键,就会显示您按下了X".我整天都在忙着,但是每次按一个键,都会收到"未捕获的TypeError:对象#没有方法'postMessage'".

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'".

所有错误均在Javascript中; Native Client C ++模块可以正常工作.

The errors are all in the Javascript; the Native Client C++ module works fine.

文档开头的Javascript:

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');}
}

页面中:

<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标签中……没有任何效果.

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.

我有使用C/C ++和PHP的经验,但是我的Javascript确实很弱.我想我肯定缺少一些基本的和显而易见的东西.

I have experience with C/C++ and PHP but my Javascript is really weak. I think I must be missing something fundamental and obvious.

推荐答案

已解决.在页面的其他地方,包含游戏模块的DIV的内容已更改,从而将模块从内存中删除.

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

这篇关于Chrome Native Client,使用onKeyDown事件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆