Path: csiph.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: John-Paul Stewart Newsgroups: comp.lang.javascript Subject: Re: ASCII unicode values Date: Thu, 26 Nov 2020 19:02:51 -0500 Lines: 32 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net W01wfg5Ynuk/BCh/B3dGRAFyb68K0QYkrZ/iR05YP8h+yAQBWW Cancel-Lock: sha1:voDrsucOxNf+LDmNFmi6uBbfdxc= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 In-Reply-To: Content-Language: en-CA Xref: csiph.com comp.lang.javascript:39202 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