Wuhtzu Posted October 27, 2010 Share Posted October 27, 2010 Hi guys I've struggled with a very annoying problem for the last two hours. I simply can't get IE8 to trigger onKeyDown and onKeyPress. Included is a minimal working example. It works great in Firefox, Safari, Chrome and Opera. I vaguely remeber it triggering in IE8 at some point during this evening but if it did I've forgotten what made it work. The body tag has onKeyPress and onKeyDown call the function test() which simply displays an alert. What am I doing wrong? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" dir="ltr"> <head> <title>ie8 onkeyup onkeypress test</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <script type="text/javascript"> function test(e) { alert("ie8 come on"); } </script> </head> <body onKeyPress="return test(event)" onKeyDown="return test(event)"> <p>some text</p> </body> </html> Best regards and thank you - Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/217052-problem-with-onkeypress-and-onkeydown-in-ie8/ Share on other sites More sharing options...
Wuhtzu Posted October 28, 2010 Author Share Posted October 28, 2010 Problem solved I didn't anticipate IE to actually restrict the "onKeyPress" and "onKeyDown" to the actual area of the <body>-tag. So The reason why I didn't see anything was because my mouse wasn't hovering over the body area which was only surrounding the "some text"-text in the upper left corner. So in some way IE actually behaved quite rationally and it turns out that either Safari, Firefox and Opera automatically enlarges the <body> to fill the whole screen or events on the body tag fire even though the mouse isn't actually hovering over the body. On the other hand, why should keyboard related events have anything to do with the position of the cursor? You can't really in my opinion determine if the keyboard is in the <body>-area or not. You could of cause base it on the markers position (not cursor, marker) but that's also a bad idea. So I guess i like firefox, safari and operas handling better. Thanks for reading and thinking guys! Quote Link to comment https://forums.phpfreaks.com/topic/217052-problem-with-onkeypress-and-onkeydown-in-ie8/#findComment-1127590 Share on other sites More sharing options...
Wuhtzu Posted October 28, 2010 Author Share Posted October 28, 2010 Ive updated the link to more clearly reflect this effect by enlarging the body and giving it a border: http://wuhtzu.dk/sandbox/onkey_ie8.html Quote Link to comment https://forums.phpfreaks.com/topic/217052-problem-with-onkeypress-and-onkeydown-in-ie8/#findComment-1127599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.