OnKeyDown and String.FromCharCode

I have a question about the OnKeyDown event. The OnKeyDown event gives a KeyCode, but I don’t know exactly what code is given. Basically, I used the String.FromCharCode method to get the real character from what I thought was ASCII code. It worked fine until I tried with numbers from numpad. If I type β€œ2” using the key above β€œw” that matches the window but with β€œ2” from numpad, the specified KeyCode is 98 (which is Ascii's β€œb” code).

I looked at this page and there is the same problem. This example is intended to prevent the user from entering numbers. It works great with numbers on top of the first character (due to the lack of a better name), but you can enter numbers using the numeric keypad.

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

THX ...

+4
source share
4 answers

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); } 

Real-time example: http://jsfiddle.net/yahavbr/vwc7a/

+7
source

The numbers returned from the key code card are in the next set, as defined here . This set is not ASCII compliant, but is similar in some respects. I will look, see if I can find additional information on how to get characters from numbers.

I also recommend using e.which instead of e.keyCode

And as another comment, w3schools never trusts.

Take a look at e.originalEvent.keyIdentifier , it seems to contain some unicode. But it still displays numpad in AH instead of 0-9. I think he just hates numpad. There must be a bnlean isnumpad flag somewhere. The DOM3 API has a logical numeric keypad.

Turns e.originalEvent.keyLocation === 3 when you press 1 on the numeric keypad.

From specification W3

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

So, the keyword === 3 maps to DOM_KEY_LOCATION_NUMPAD. Then you will need to catch numpad manually. you can subtract 48 from the code key for numpad to map AH to 0-9

[Big disclaimer]

This is what FF and Chrome do. God knows what IE is doing, you can defeat this beast yourself. I have no doubt that he is doing something completely different from the W3 spec.

+3
source

keyCode and charCode are different animals. keyCode refers to the keyboard and key opening, while charCode actually gives the ascii character code.

In addition, there are many bad things in different implementations of key events. I always liked this page as a good source: http://unixpapa.com/js/key.html

Edit: As Rainos says, skip w3schools when it appears in Google search results.

+3
source

FF (v.10.0, at least) does not have the event property "originalEvent.KeyLocation", like Chrome. You can use this snippet of JS code to recognize numpad numbers if your text box should only be a numeric number . This is also a fully cross browser:

 var my_char=e.which; if(e.which>95 && e.which<106){ my_char=(e.which-96).toString(); } 
0
source

Source: https://habr.com/ru/post/1336969/


All Articles