3

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's explicit event object (window.event) and Firefox's 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 number pad (the right side of the keyboard, not the shift+8 one) the alert says "j" insted of "*" why is that happening?

alexP
  • 3,672
  • 7
  • 27
  • 36
Lotan Amit
  • 31
  • 1
  • Which browser are you trying it in? Do different browsers give different results? – void May 13 '15 at 09:15
  • Is that your non-working code? `actualkey == "8"` couldn't be 8 except you press the "8" on keyboard. Because you're converting the `charCode` to a String in this code. This works fine for me: [Fiddle](http://fiddle.jshell.net/Pisi2012/63ekf8m0/) – alexP May 13 '15 at 09:21
  • 1
    possible duplicate of [Get Character value from KeyCode in Javascript…then trim](http://stackoverflow.com/a/5829387/1176601) – Aprillion May 13 '15 at 09:42
  • possible duplicate of [Get Character value from KeyCode in Javascript...then trim](http://stackoverflow.com/questions/1772179/get-character-value-from-keycode-in-javascript-then-trim) – Aprillion May 13 '15 at 09:43

1 Answers1

0

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 numpad) it produces the keyIdentifer U+004A which, in the UTF-8 Charset, equals the 'LATIN CAPITAL LETTER J'.

Also note that my code only works in Chrome.

In Firefox the evt.key correctly produces an Asterix and in both browsers evt.keyCode produces the Char Code 106, which according to google should equal 'multiply' but if converted using javascript actually produces 'j'.

This seems to be a javascript related issue.

I hope this information helps somebody out there.

Greetings spitterfly

Edit: Found the following on the mozilla developer web page:

Although most common Unicode values can be represented with one 16-bit number (as expected early on during JavaScript standardization) and fromCharCode() can be used to return a single character for the most common values (i.e., UCS-2 values which are the subset of UTF-16 with the most common characters), in order to deal with ALL legal Unicode values (up to 21 bits), fromCharCode() alone is inadequate. Since the higher code point characters use two (lower value) "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 these higher valued characters.

Mozilla Reference fromCharCode

spitterfly
  • 28
  • 1
  • 4