Handling cross browser key press events in JavaScript

This is something that I use to catch presses on the return key, so I can submit a form easily. nn=(document.layers)?true:false; ie=(document.all)?true:false; function keyDown(e) { var evt=(e)?e:(window.event)?window.event:null; if(evt){ var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0)); if(key=="13") document.forms[0].submit(); } } document.onkeydown=keyDown; if(nn) document.captureEvents(Event.KEYDOWN);

Further reading: Key Press Event Handling, The JavaScript Event Model: Part Two, page 8 – Keyboard Events.

The above can also be used to for example catching ALL key events to generate instant preview of the document being edited, by writing the document content to a DIV or something…