最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - OnKeyDown and String.FromCharCode - Stack Overflow

programmeradmin3浏览0评论

I've a question concerning the OnKeyDown event. The OnKeyDown event gives a KeyCode but I don't know exactly what kind of code is given. Basically, I was using the String.FromCharCode method to get the real character from what I thought was the ASCII-Code. It worked fine until I tried with the numbers from the numpad. If I type the 2 using the key above w that's ok, but with the 2 from the num-pad the KeyCode given is 98 (which is b ASCII code).

I was looking at this page and there's the same problem. The example is supposed to prevent the user from typing numbers. It works perfectly fine with the numbers on top of the first character (for lack of a better name), but you can type numbers using the num-pad.

Do you have any idea what the problem is? (is this really the ASCII code? Am I using the wrong event ? )...

I've a question concerning the OnKeyDown event. The OnKeyDown event gives a KeyCode but I don't know exactly what kind of code is given. Basically, I was using the String.FromCharCode method to get the real character from what I thought was the ASCII-Code. It worked fine until I tried with the numbers from the numpad. If I type the 2 using the key above w that's ok, but with the 2 from the num-pad the KeyCode given is 98 (which is b ASCII code).

I was looking at this page and there's the same problem. The example is supposed to prevent the user from typing numbers. It works perfectly fine with the numbers on top of the first character (for lack of a better name), but you can type numbers using the num-pad.

Do you have any idea what the problem is? (is this really the ASCII code? Am I using the wrong event ? )...

Share Improve this question edited Jan 19, 2023 at 23:09 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 25, 2011 at 12:55 LB40LB40 12.3k17 gold badges74 silver badges109 bronze badges 3
  • thanks for all the answers, however I'm using a ponent provided by an editor providing only OnKeyDown...i'm screwed. I didn't know w3school was bad... – LB40 Commented Jan 25, 2011 at 13:25
  • You can use onkeydown just fine. It's not ideal. But if you have the event object you can get event.which and you should be able to get the keylocation and block the numpad pletely if you wanted to. – Raynos Commented Jan 25, 2011 at 13:48
  • I don't have the whole event but only the keyCode and 2 bool to say control is pressed and shift is pressed. – LB40 Commented Jan 25, 2011 at 16:29
Add a ment  | 

4 Answers 4

Reset to default 7

To prevent numbers use onkeypress instead:

onkeypress="return PreventDigits(event);"
...
function PreventDigits(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode || evt.which;7
    var num = parseInt(String.fromCharCode(keyCode), 10);
    return isNaN(num);
}

Live example: http://jsfiddle/yahavbr/vwc7a/

keyCode and charCode are different animals. keyCode relates to the keyboard and the key loayout, whereas the charCode actually gives the ascii character code.

Further there is a lot of bad in the different implementations of key events. I always liked this page as a good source: http://unixpapa./js/key.html

Edit: As Raynos says, skip w3schools when it es up in Google searches.

The numbers returned from keycode map to the following set as defined here. This set does not correspond to ASCII but is similar in some regards. I will take a look and see if I can find more information about how to get characters from the numbers.

Also I remend you use e.which instead of e.keyCode

And as a further ment never trust w3schools.

Take a look at e.originalEvent.keyIdentifier it seems to contain some unicode. But it still maps the numpad to A-H instead of 0-9. I think it just hates the numpad. There should be a boolean isnumpad flag somewhere. The DOM3 API does have a boolean numpad.

Turns out e.originalEvent.keyLocation === 3 when you press 1 on the numpad.

From the W3 spec

KeyboardEvent.DOM_KEY_LOCATION_STANDARD The value of the constant KeyboardEvent.DOM_KEY_LOCATION_STANDARD is 0x00. KeyboardEvent.DOM_KEY_LOCATION_LEFT The value of the constant KeyboardEvent.DOM_KEY_LOCATION_LEFT is 0x01. KeyboardEvent.DOM_KEY_LOCATION_RIGHT The value of the constant KeyboardEvent.DOM_KEY_LOCATION_RIGHT is 0x02. KeyboardEvent.DOM_KEY_LOCATION_NUMPAD The value of the constant KeyboardEvent.DOM_KEY_LOCATION_NUMPAD is 0x03

So keylocation === 3 maps to DOM_KEY_LOCATION_NUMPAD Your going to have to catch the numpad manually then. you can subtract 48 from the keycode for the numpad to map A-H to 0-9

[Big Disclaimer]

This is what FF & Chrome do. God knows what IE does, you can conquer that beast yourself. I have no doubt it does something pletely different from the W3 spec.

FF (v.10.0, at least) does not have the event property "originalEvent.KeyLocation" as Chrome has. You could use this JS piece of code to recognize numpad numbers, if your text field is supposed to be numeric only. This is also fully cross-browser:

var my_char=e.which;
if(e.which>95 && e.which<106){
    my_char=(e.which-96).toString();
}
发布评论

评论列表(0)

  1. 暂无评论