Keypress event. "*" is treated as "j"

I tried to create a javascript event that detects when the * key is pressed. code:

 function asterisk(e) {
     var evtobj = window.event ? event : e //distinguish between IE explicit event object (window.event) and Firefox implicit.
     var unicode = evtobj.charCode ? evtobj.charCode : evtobj.keyCode
     var actualkey = String.fromCharCode(unicode)
     alert(actualkey);
     if ((actualkey == "8" && evtobj.shiftKey) || actualkey == "*") {
         alert("banana");
         var tbbar = document.getElementById("tbBarcode");
         var tbam = document.getElementById("tbAmount");
         tbam.value = tbbar.value;
         tbbar.value = "";
         tbbar.focus();

     }
 }

      

when I press the * key on the numeric keypad (the right side of the keyboard, not shift + 8) the warning indicates "j" set to "*" why is this happening?

+3


source to share


1 answer


I was able to reproduce the phenomenon with the following code:

window.onkeydown=function(evt) {
    console.log(evt.keyIdentifier)
}

      

If I press the * key (also on the numeric keypad) it gives out the key Identifer U + 004A, which is UTF-8 encoded "LATIN CAPITAL LETTER J".

Also note that my code only works in Chrome.

In Firefox it evt.key

creates Asterix correctly, and in both browsers it evt.keyCode

produces Char Code 106, which according to google should equal "multiply", but if converted with javascript actually creates "j".

It looks like a javascript related issue.



Hope this information helps someone out there.

Hello spitterfly

Edit: On the mozilla developer webpage, you will find the following:

While most common Unicode values ​​can be represented by a single 16-bit number (as expected early in JavaScript standardization) and fromCharCode () can be used to return one character for most common values ​​(i.e. UCS-2 values, which are a subset of UTF -16 s most common characters) to deal with all legal Unicode values ​​(up to 21 bits), only from CharCode () are inadequate. Since the higher code point characters use two (lower values) "surrogate" numbers to form a single character, String.fromCodePoint () (part of the ES6 draft) can be used to return such a pair and thus adequately represent those higher value characters.

Mozilla link fromCharCode

0


source







All Articles