Monday 19 September 2011

Usage of Key Stroke Events In DHTML (Cross Browser)


Usage of Key Stroke Events


To capture what key was pressed works a little bit differently between the browsers. The table below shows how each browser works to capture a key stroke.
NS
Function keyDown(e) {
                     Var keycode = e.which
                     var realkey = String.fromCharCode(e.which)
alert("keycode: " + keycode + "\nrealkey: " + realkey)
}
document.onkeydown = keyDown
document.captureEvents(Event.KEYDOWN)

IE

Function keyDown() {
var keycode = event.keyCode
var realkey = String.fromCharCode(event.keyCode) alert("keycode: " + keycode + "\nrealkey: " + realkey)
}
document.onkeydown = keyDown document.onkeydown = keyDown

The Cross Browser Method
function keyDown(e) {
     if (ns4) {var nKey=e.which; var ieKey=0}
     if (ie4) {var ieKey=event.keyCode; var nKey=0}
     alert("nKey:"+nKey+" ieKey:" + ieKey)
}
document.onkeydown = keyDown
if (ns4) document.captureEvents(Event.KEYDOWN)
Note: The character sets for Netscape and Internet Explorer differ. In general, all letters, numbers, symbols, Space, and Enter will work fine

No comments:

Post a Comment

Please Give Your Valuable Comments on this Topic

Archives