View Full Version : capturing keystrokes for mozilla

04-04-2003, 10:18 AM
I've written this small script which handles onkeypress events for a webpage but i'd like to upgrade it so it is also compatible with Mozilla/Netscape 6. I was hoping that someone might be able to post up how to capture onkeypress events for mozilla.

I actually validate the variable "key" (checking to see if it is a letter of the alphabet, etc) later on in catchKey()

var isNS4 = (document.layers);

document.onkeypress = catchKey
if (isNS4) document.captureEvents(Event.KEYDOWN)

function catchKey(e) {
var keyNum = (isNS4) ? e.which : event.keyCode;
key = String.fromCharCode(keyNum).toLowerCase();


Many thanks for your help.


04-04-2003, 11:07 AM
Oh, for the third time or fourth time in a week's time, I'll post a link to the only page you'll need for anything that has to do with key events: <http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/KeyEvent.htm>

Until the day all browsers support DOM Events...

04-04-2003, 11:25 AM
Many thanks!

04-04-2003, 11:50 AM
Originally posted by liorean
Until the day all browsers support DOM Events...

There is no current DOM Events way of handling the keyboard. DOM3 Events has TextEvent, but that's not in Candidate Recommendation yet, therefore browsers shouldn't implement it yet.

04-04-2003, 12:56 PM
It also has keydown and keyup. Anyway, in difference from css - where a change may force you to rewrite a lot of underlying code, which may have unintended effects on other rules and properties - DOM has a quite stable ground to built upon and implementating a feature is a good way to test if the feature and how it works is sound, or if it needs improvement. A change in DOM is less likely to affect other interfaces or features, and you usually can rewrite a method or property for a DOM interface without interfering with the rest of the DOM.