homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Browsers / Opera Browser Usage and Support
Forum Library, Charter, Moderator: open

Opera Browser Usage and Support Forum

'keyCode' undefined in Opera 6

 5:48 pm on May 21, 2002 (gmt 0)

How do I detect the onKeyUp event and get the keyCode returned in Opera 6?

I have tried several different alternatives, using both IE style 'event.keyCode' and NN style 'e.keyCode' (where 'e' is set by the JavaScript function).

The script is run, but 'keyCode' is still undefined ..




 7:02 pm on May 21, 2002 (gmt 0)

Opera's website [opera.com] says that keyCode is supported in Opera 5 and up. But...

Here'a a demo page with a Key Press Test [userfirst.net] - it uses event.keyCode in the jscript. In Opera, IDENTIFIED AS OPERA, the test always says "key is undefined".

But when Opera identifies itself as IE5, then its jscript object support (as opposed to javascript) kicks in ... however, it only works for a very few keys.

The O'Reilly book says this about keyCode: "The DOM Level 2 does not standardize key events (although DOM Level 3 is working on this) and has no equivalent to these properties."

For now, you may not be able to get Opera to work here as you want.

(edited by: tedster at 8:39 pm (utc) on May 21, 2002)


 7:03 pm on May 21, 2002 (gmt 0)

Hello DrDoc,
Here is Opera's Support for JavaScript and JScript document and browser objects: [opera.com...]

It shows keyCode being supported beginning with Opera 5 (Tedster beat me to it! ;))

I'm not sure how to detect the onKeyUp event however. Has anyone else had much experience with limitations when scripting for Opera?


 8:32 pm on May 21, 2002 (gmt 0)

Opera does weird stuff with onkeypress. First try the following (the following script was influenced by various sources and is not wholly original):

<script type="text/javascript">
window.onload = function() {
function showKey(e) {
var key;
var code;
if (!e) var e = window.event; // set var e for ie
if (e.keyCode) code = e.keyCode; // ie and mozilla/gecko
else if (e.which) code = e.which; // ns4 and opera
var key = String.fromCharCode(code);
window.status = "Key=" + key;
return true;

The keypress result will show in the window status bar. Now try changing "document.onkeyup" to "document.onkeydown". Same behavior. Now try it with "document.onkeypress". Weirdness ensues. Lowercase "a" through "i" translate to "1" through "9," and other lowercase keys are erratic. The only way you can get reliable behavior is if you use the "Shift" key.

It probably has something to do with the fact that Netscape and IE handles keypresses in different ways, and Opera is trying to support both (or neither) and is getting confused.

It also seems that Opera's own keyboard shortcuts create difficulties for onkeypress.

So ... if you can get away with it, use onkeydown or onkeyup. They seem much safer.

My $0.02.

p.s., if you are calling it inline from a html element, it would be onkeyup="showKey(event);"


 8:03 am on May 27, 2002 (gmt 0)

Well, Opera detects keys like a-z, numbers etc .. even the Shift key .. But it won't detect keys like PgDn, PgUp, Down, Left, Right, Up ..

Unfortunately those are the ones I need ..

Global Options:
 top home search open messages active posts  

Home / Forums Index / Browsers / Opera Browser Usage and Support
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved