Keycodes on keydown event

Hi,

I want to only allow numbers (0-9) to be entered into a textbox. I am just testing some code, but i discovered that pressing numbers on the numerical pad on the keyboard creates different keycodes than using the ordinary numbers on a keyboard.  Why is this?  Also when i use String.fromCharCode(keyCode), and press a letter, the capital of that letter appears, and say if pressing the single quote key, i get a completely different character from it. Whats going on?

Thanks,
Mike
LVL 1
Xavior2K3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
in the onKeyDown event handler you do not get the character of the pressed key, but you get the scan code of the key.
So you had to evaluate also the shift state of the keyboard and to translate the differencies betwean Mozilla and IE codes.

Or you use this:

<input type="text" name="myNumber" onKeyUp="if(this.value.match(/\D/))this.value=this.value.replace(/\D/g,'')">

0
Xavior2K3Author Commented:
What do you mean by scan code of the key?
0
ZvonkoSystems architectCommented:
Keyboard has not a value for a key. The value is generated from the asociated Codepage of the current keyboard language, for example, in Hebrew there are no lowercase latin letters.
But the keys have numbers which you call keyCode. That keyCode is already translated number from the key code. For example the character A has not to be the far left key on the keyboard.  Therefore is scan code the code before the keyCode of the key. The keyboard driver translates the scan code to apropriate keyCode acording to actual used code page. But the scan code is the untranslated raw code of the key.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Xavior2K3Author Commented:
Ok i see, so the keyCode returns the scan code? How can i get an ASCII value that corresponds to the key pressed then?

Should i use the keypress event? I have tries the keypress event but it isnt working, it gets fired when pressing any key, but only generates a keycode when say backspace is pressed, not for any letter or number keys?
0
ZvonkoSystems architectCommented:
It depends what bahavior you need.
For backspace it is easy to use it in onKeyDown event handler, but for all characters you have to make a compromise betwean many statemets which you have to maintain in the future and the onKeyUp event handler which presents the maped keyCode to the character value.
I preffer using the onKeyUp event handler.

0
Xavior2K3Author Commented:
Ok, well im trying to go for a nice looking input box and i would like to use onKeyDown instead of onKeyUp.  This is because using onKeyUp would display the key they pressed for a split second, and i think wouldnt look as good as stopping the keyboard event before it inserts the character in the textbox.

Would the scan codes be the same for all types of keyboards and all types of regoinal settings and such... so zero on the number pad would always generate the same scancode? And also could you explain why it would need maintaining in the future?

Thanks alot for your help!
0
ZvonkoSystems architectCommented:
You need to maintain the table not because of scan codes but because of the different keyCodes from same scan code in Mozilla and IE.
http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.