title: How do you convert a Javascript keyCode to a charCode?
body: <p>In a Javascript <strong>keyboard event</strong>, a code is given for the key pressed, however, <em>it is not an ASCII or UTF-8 character code,</em> rather, it is a code for the keyboard key. Sometimes the keyCodes happen to match up with ASCII/UTF-8 codes, sometimes they do not.</p>
  <p>Is there a way, given a Javascript key code, (accessed via <code>e.keyCode</code> or <code>e.which</code>) to get the corresponding ASCII or UTF-8 charcode?</p>
  <p><a href="https://jsfiddle.net/vsh6e5f9/" rel="nofollow noreferrer">Here's a working JSFiddle</a> to demonstrate what I mean, or run the snippet below.</p>
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>document.addEventListener("keyup", function(e) {
  
    document.querySelector(".key").innerHTML = e.key;
    document.querySelector(".keyCode").innerHTML = e.keyCode;
    document.querySelector(".UTF").innerHTML = String.fromCharCode(event.keyCode);
  
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>code {
    background-color: #e2aec8ab;
    border: solid 1px gray;
    border-radius: 3px;
    padding: 0 .5em;
    min-height: 1em;
    min-width: .2em;
    margin: 0 .1em;
  }
  
  .output {
    margin: 2em;
    border: solid 1px lightgray;
    border-radius: 3px;
    background-color: rgba(200, 250, 230, .3);
    padding: 1em;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Type here. Try some alpha letters as well as special keys like &lt;code&gt;`&lt;/code&gt;
  &lt;code&gt;-&lt;/code&gt;
  &lt;code&gt;=&lt;/code&gt;
  &lt;code&gt;[&lt;/code&gt;
  &lt;code&gt;]&lt;/code&gt;
  &lt;code&gt;[ENTER]&lt;/code&gt;
  &lt;code&gt;;&lt;/code&gt;
  &lt;code&gt;'&lt;/code&gt;
  
  
  &lt;div class="output"&gt;
    You typed &lt;code class="key"&gt;&lt;/code&gt; which is Javascript &lt;b&gt;&lt;/b&gt;keyCode&lt;/b&gt; &lt;code class="keyCode"&gt;&lt;/code&gt;, in UTF that would be &lt;code class="UTF"&gt;&lt;/code&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Two examples:</p>
  <ol>
  <li>Type <code>2</code> on the keyboard</li>
  <li>Capture the event. <code>event.keyCode</code> is 50.</li>
  <li>The UTF character 50 is <code>2</code> (<a href="https://www.fileformat.info/info/unicode/char/0032/index.htm" rel="nofollow noreferrer">DIGIT TWO</a>)</li>
  </ol>
  <p>but:</p>
  <ol>
  <li>Type <code>[</code> on the keyboard</li>
  <li>Capture the event. <code>event.keyCode</code> is 219.</li>
  <li>The UTF character 219 is <code>Û</code> (<a href="http://www.fileformat.info/info/unicode/char/fb/index.htm" rel="nofollow noreferrer">LATIN CAPITAL LETTER U WITH CIRCUMFLEX</a>)</li>
  <li>The charcode I wish the event contained is <code>91</code> which corresponds to <a href="https://www.fileformat.info/info/unicode/char/005b/index.htm" rel="nofollow noreferrer">LEFT SQUARE BRACKET</a>.</li>
  </ol>
  <p>How do I convert Javascript keyCodes (e.g. <code>219</code>) into UTF-8 charCodes (e.g. <code>91</code>)?</p>