title: Translate Javascript keyCode into charCode for non-U.S. keyboard layout (i.e. azerty)
body: <p>Quick background:</p>
  
  <ul>
  <li>when a key is pressed in a browser, three events are generated: <strong>keyDown</strong>, <strong>keyPress</strong> and <strong>keyUp</strong>.</li>
  <li><strong>keyDown</strong> and <strong>keyUp</strong> have a <strong>keyCode</strong> property which is approximately the physical key pressed.</li>
  <li><strong>keyPress</strong> also has <strong>charCode</strong> property set which takes into account modifier keys and keyboard layout (A and a have same keyCode but a different charCode).</li>
  <li>all three events have properties that indicate which modifier keys were pressed during those events.</li>
  </ul>
  
  <p>I'm the main <a href="https://github.com/kanaka/noVNC" rel="noreferrer">noVNC</a> developer and I have a tough problem: noVNC needs the translated <strong>charCode</strong> value without using the <strong>keyPress</strong> event for the following reasons:</p>
  
  <ul>
  <li>noVNC needs to send the keyDown and keyUp events separately to the VNC server (otherwise it's not a completely functional VNC client).</li>
  <li>more importantly, noVNC needs to prevent the default keyboard actions while connected which means calling the <strong>preventDefault()</strong> method of the <strong>keyDown</strong> event. This has the side-effect of also preventing the <strong>keyPress</strong> event from firing.</li>
  </ul>
  
  <p>Due to differences in keyboard layouts (i.e. different keyCode to charCode mappings) I've determine that noVNC will need a lookup table for different keyboard layouts.</p>
  
  <p><strong>But here is the real problem:</strong> on alternate layouts, some different physical keys have the <strong>SAME</strong> keyCode. For example, with an <strong>azerty</strong> (French) keyboard layout the '-' (dash) and '_' underscore keys both generate keyCode 189. Ack!!!</p>
  
  <p>So ... how do I get proper keyCode to charCode mapping and prevent default browser actions at the same time?</p>
  
  <p>BTW, I suspect the solution to this will be applicable to other interactive web applications and HTML5 games since you often want to be able to know full information about the key pressed without triggering any additional browser response to that keypress.</p>
  
  <p>Useful links:</p>
  
  <ul>
  <li><a href="http://kanaka.github.com/noVNC/noVNC/tests/keyboard.html" rel="noreferrer">Here</a> is an useful test page that show the three events and some other useful properties.</li>
  <li><a href="http://unixpapa.com/js/key.html" rel="noreferrer">Summary</a> of the crazy state of key events in Javascript (thanks @Tim)</li>
  <li>Quirksmode <a href="http://www.quirksmode.org/js/keys.html" rel="noreferrer">Detecting keystrokes</a></li>
  <li>Quirksmode <a href="http://www.quirksmode.org/dom/events/tests/keys.html" rel="noreferrer">Events - key events</a></li>
  <li><a href="https://github.com/kanaka/noVNC/issues/#issue/21" rel="noreferrer">noVNC issue</a> with more discussion of the problem.</li>
  </ul>
  
  <p><strong>Solution</strong>: see my post below.</p>