Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39202

Re: ASCII unicode values

From John-Paul Stewart <jpstewart@personalprojects.net>
Newsgroups comp.lang.javascript
Subject Re: ASCII unicode values
Date 2020-11-26 19:02 -0500
Message-ID <i2au1bFmburU1@mid.individual.net> (permalink)
References <f0dd6201-439b-4bfa-a5b5-488fdd23e0c2n@googlegroups.com>

Show all headers | View raw


On 2020-11-26 10:35 a.m., Jonas Thörnvall wrote:
> Why does not javascript follow the OS and language implementation of
> its reflection on keypresses, why does it use some internal
> "charcode?" set where your keypresses do not follow the way you use
> them?
> 
> key = e.keyCode ? e.keyCode : e.which; 
> character= String.fromCharCode(key);
You are mixing up keyCodes and charCodes.  String.fromCharCode() expects
its parameter(s) to be charCode(s), as you should be able to see from
the name.  Those charCodes are "UTF-16 code units" according to the docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

But what you're passing is a keyCode instead.  That is "a system and
implementation dependent numerical code identifying the unmodified value
of the pressed key" according to its documentation:

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode

You are clearly very confused since you are passing a system-dependent
keyCode to a function that is expecting a UTF-16 charCode.  They are not
the same thing.

You might want key = e.charCode instead.

Or, since both charCode and keyCode are deprecated and non-standard, you
should probably read up on the key property instead, as was suggested by
Michael Haufe in his post to a different thread back on 2020-11-11.

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ASCII unicode values Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-26 07:35 -0800
  Re: ASCII unicode values Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-26 08:15 -0800
    Re: ASCII unicode values Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-26 12:07 -0800
    Re: ASCII unicode values "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-11-26 22:25 +0100
      Re: ASCII unicode values Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-26 13:46 -0800
        Re: ASCII unicode values "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-11-27 00:08 +0100
  Re: ASCII unicode values John-Paul Stewart <jpstewart@personalprojects.net> - 2020-11-26 19:02 -0500
    Re: ASCII unicode values Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-27 10:43 -0800
      Re: ASCII unicode values Jonas Thörnvall <jonas.thornvall@gmail.com> - 2020-11-28 13:54 -0800
        Re: ASCII unicode values luserdroog <luser.droog@gmail.com> - 2020-11-28 18:59 -0800

csiph-web