title: How can I distinguish left- and right- shift, ctrl, and alt keys onkeyup in Chrome with Javascript
body: <p><strong>UPDATE: This question is not a duplicate of another question. It does not have an answer elsewhere.</strong></p>
  <p>I was inspired by this question <a href="https://stackoverflow.com/questions/22029033/can-javascript-tell-the-difference-between-left-and-right-shift-key/22029109#22029109">Can javascript tell the difference between left and right shift key?</a> to make a keyboard input class for a game kit, to distinguish key presses for individual keys.</p>
  <p>The rub is that the <code>onkeydown</code> and <code>onkeyup</code> events in Javascript have identical keycodes for <kbd>left shift</kbd> or <kbd>right shift</kbd>, as well as <kbd>left ctrl</kbd>/<kbd>right ctrl</kbd> and <kbd>left alt</kbd>/<kbd>right alt</kbd>.</p>
  <p>Per the response to the above question, modern browsers now return <code>event.location</code>, indicating whether these keys are left or right. IE and Firefox return the appropriate location value for these keys <code>onkeydown</code> and <code>onkeyup</code>.</p>
  <p>But Chrome gives the correct value only <code>onkeydown</code>. In Chrome, <kbd>shift</kbd>, <kbd>ctrl</kbd>, and <kbd>alt</kbd> all give a location <code>onkeyup</code> of <code>KeyboardEvent.DOM_KEY_LOCATION_STANDARD</code>, which is ambiguous.</p>
  <p>Here is an example sandbox: <a href="http://jsfiddle.net/ySkn8/1/" rel="nofollow noreferrer">jsfiddle</a>.</p>
  <p>If you hit <kbd>shift</kbd>, <kbd>ctrl</kbd>, or <kbd>alt</kbd>, the <code>onkeydown location</code> will show correctly in Chrome (and all other browsers), but in Chrome the <code>onkeyup location</code> will be 'standard'.</p>
  <p>relevant code:</p>
  <pre><code>window.onkeydown = function(event)
  {
      var o = 'event = onkeydown, which = ' + event.which + ', location = ';
      
      switch(event.location)
      {
          case KeyboardEvent.DOM_KEY_LOCATION_STANDARD: o += 'standard'; break;
              
          case KeyboardEvent.DOM_KEY_LOCATION_NUMPAD: o += 'numpad'; break;
  
          case KeyboardEvent.DOM_KEY_LOCATION_LEFT: o += 'left'; break;
              
          case KeyboardEvent.DOM_KEY_LOCATION_RIGHT: o += 'right'; break;
      }
      
      var outputSpan = document.getElementById('output');
      
      outputSpan.innerHTML = o;
  };
  
  window.onkeyup = function(event)
  {
      var o = 'event = onkeyup, which = ' + event.which + ', location = ';
      
      switch(event.location)
      {
          case KeyboardEvent.DOM_KEY_LOCATION_STANDARD: o += 'standard'; break;
              
          case KeyboardEvent.DOM_KEY_LOCATION_NUMPAD: o += 'numpad'; break;
  
          case KeyboardEvent.DOM_KEY_LOCATION_LEFT: o += 'left'; break;
              
          case KeyboardEvent.DOM_KEY_LOCATION_RIGHT: o += 'right'; break;
      }
      
      var outputSpan = document.getElementById('output');
      
      outputSpan.innerHTML = o;
  };
  </code></pre>
  <p>Is this a bug, or somehow desired behavior? In light of this, is there another way to distinguish between the left and right side keys onkeyup?</p>
  <p>UPDATE:</p>
  <p>I worked around this by listening to which side the <code>keydown</code> came from, remembering it, and assuming that the subsequent <code>keyup</code> came from the same side. This isn't great, since it isn't necessarily true all the time.</p>
  <p>This question is not a duplicate of <a href="https://stackoverflow.com/questions/4590883/how-can-i-tell-if-an-event-comes-from-right-ctrl-key">How can I tell if an event comes from right Ctrl key?</a> which asked how to distinguish <kbd>left-ctrl</kbd> from <kbd>right-ctrl</kbd> key presses. That question is in fact answered by the question I originally referenced: <a href="https://stackoverflow.com/questions/22029033/can-javascript-tell-the-difference-between-left-and-right-shift-key/22029109#22029109">Can javascript tell the difference between left and right shift key?</a></p>
  <p>This appears to be a Chrome-specific defect in their API behavior, and since there isn't a full work-around, I might go file a bug report with the Chromium project.</p>