[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Keycodes on keydown event

Posted on 2006-04-19
10
Medium Priority
?
1,050 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Xavior2K3
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 16491721
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
 
LVL 1

Author Comment

by:Xavior2K3
ID: 16491793
What do you mean by scan code of the key?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 16491881
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Xavior2K3
ID: 16491933
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 16492065
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
 
LVL 1

Author Comment

by:Xavior2K3
ID: 16492158
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
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 16492698
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question