开发者

OnKeyDown and String.FromCharCode

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 t开发者_Python百科his 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 ? )...


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.net/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.com/js/key.html

Edit: As Raynos says, skip w3schools when it comes 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 recommend you use e.which instead of e.keyCode

And as a further comment 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 completely 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜