Chrome Native Client, errors using onKeyDown event

谁都会走 提交于 2019-12-11 11:11:50

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9244911/chrome-native-client-errors-using-onkeydown-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!