Question

I am currently trying to capture the key-code in a keypress event in JavaScript for a text box. I'm using the following line to detect the keycode:

var iKey = e.keyCode || e.charCode || e.which || 0;

However, the arrow keys are coming up as ', % and so forth.

Can anyone shed some light on this? Any help would be greatly appreciated.

Was it helpful?

Solution

You should just be doing this:

var iKey = e.keycode || e.which || 0;

Quirksmode explains your problem well:

The two properties are keyCode and charCode. Put (too) simply, keyCode says something about the actual keyboard key the user pressed, while charCode gives the ASCII value of the resulting character. These bits of information need not be the same; for instance, a lower case 'a' and an upper case 'A' have the same keyCode, because the user presses the same key, but a different charCode because the resulting character is different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top