February 28, 2012

how to get keycode value without passing event in JS for firefox?

Question by sadique

I have a JSP page which has an event onkeydown and calla a Ajax function ajaxFn(this), this function calls a series of functions and then comes to common function ajaxInteraction(response,ajaxObj).
In this common function there is a subfunction which determines the keycode value,

ajaxObj.onkeydown = function(){

var keynum;
//  keynum = event.which;

if(window.event) // IE
{
    keynum = event.keyCode;
}

which recognises only the value from IE and does not recognises Mozilla.
I want to capture the keycode values in the same function for mozilla.
Am not able to capture the value using event.which since event is not passed here from JSP.

P.S: i cannot change the existing code.

Answer by Starx

Events are the only method through which you will able read user’s interaction.

There is no other way to do this, than to use events.

For Firefox use event.charCode

var code = event.charCode ? event.charCode : event.keyCode;

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!