Keypress in javascript in IE
I have a Flex application. It running in a player on the HTML-page. I need catch the key press events and prevent IE browser from acting like it wants. Here's some code:
Actually, the part where player was layed ..
<html>
<head>
</head>
<body scroll="no" onkeydown=keypress(event)>
<noscript>
<object id="app" width="100%" height="100%" onkeydown="keypress(event)" onkeypress="keypress(event)">
//some params
</object>
</no开发者_如何学JAVAscript>
</body>
</html>
And here's the one, where I'm trying in a different ways to catch key input:
<script language="JavaScript" type="text/javascript">
function keypress(e) {
alert("Hello from keypress");
}
function init() {
//index
alert("Init!!!");
document.getElementById('app').onkeydown = function() {
alert("Key Pressed - 1");
};
document.onkeydown = function() {
alert("Key Pressed - 2");
};
document.getElementById('app').onkeypress = function() {
alert("Key Pressed - 3")
};
document.onkeypress = function() {
alert("Key Pressed - 4")
};
window.onkeydown = function() {
alert("Key Pressed - 5");
};
window.onkeypress = function() {
alert("Key Pressed - 6");
};
document.body.onkeypress = function() {
alert("Key Pressed - 7")
};
document.body.onkeydown = function() {
alert("Key Pressed - 8")
};
if (window.addEventListener) {
window.addEventListener('keypress', keypress, false);
} else if (window.attachEvent) {
window.attachEvent('onkeypress', keypress);
} else {
window.onkeypress = keypress;
}
}
window.onload = init;
document.onload = init;
</script>
I wasn't going to use them all together, just gathered them all to show you that I've tried almost everything (also including all this with 1 parameter).
The problem is that the only Alert I'm getting is "Init!!!". What's wrong with it or what I'm doing wrong?
Any help would be greatly appreciated.
Edit: I get the "Init" message before the player loads it's content .. maybe the problem is somewhere there?
In Internet Explorer, events that occur within an embedded control in the <object>
element do not fire equivalent events on the DOM object. They are consumed by the embedded control, and it is that control's responsibility to handle them accordingly.
This means, when your embedded Flex application is focussed, your JavaScript code will not be able to handle any key events.
精彩评论