Hi,
I've been looking up lots of documentation and trying to work out a
cross-platform method of capturing a keyboard event and working out
which key was pressed. From what I can find, there doesn't appear to be
any standardised keyboard event interface other than this DOM 3 Events
W3C Working Group Note [1]. However, it is only a note and doesn't
appear to be implemented in any browser. Is there another standard that
I've missed?
The Gecko DOM Reference lists event.keyCode [2], but this doesn't appear
to be implemented in Firefox 1.0.2, as e.keyCode in the following
returns 0, regardless of the key pressed.
function test(e) {
if (!e) {
e = event;
}
alert(e.keyCode);
}
document.onkeypress = test;
That works in IE and Opera, but IE needed the e = event statement, as it
seems IE doesn't pass an event object to the funciton like Mozilla and
Opera do.
I noticed Eric Meyer's S5 script makes use of key.which in function
keys(key) {...}
key.which seems to work in Firefox and Opera, but not IE, although it
does in S5 because it sets:
key.which = key.keyCode
However, I can't find any documentation for this property anywhere, on
either the Mozilla or Opera sites.
So, my question is, what is the most interoperable method to determine
the key pressed that works in at least Mozilla, Opera, IE and Safari
(though, I don't have Safari available to test in), but preferably that
works in most (if not all) modern browsers available. I generally like
to avoid proprietary extensions, but it seems (as there I don't believe
there is as official standard yet) that I may have no choice in the
matter :-(, so please correct me if I am wrong about this.
[1]
http://www.w3.org/TR/2003/NOTE-DOM-L...nts-Interfaces
[2] http://www.mozilla.org/docs/dom/domr...4.html#1003563
--
Lachlan Hunt
http://lachy.id.au/
http://GetFirefox.com/ Rediscover the Web
http://GetThunderbird.com/ Reclaim your Inbox