当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Chrome Native Client, errors using onKeyDown event

问题描述:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: