title: Cross-browser keyboard event handler
body: <p>I have a keypress handler that I borrowed that used to work across browsers but now no longer works on webkit browsers.  Can anyone offer suggestions how to fix?</p>
  
  <p>Here is the handler:</p>
  
  <pre><code>function handleKeys(e) {
      //credit: http://santrajan.blogspot.com/2007/03/cross-browser-keyboard-handler.html
      var character;
      var evt = (e) ? e : window.event;       //IE reports window.event not arg
      if (evt.type == "keydown") {
          character = evt.keycode;
          if (character &lt; 16 ||                    // non printables
              (character &gt; 16 &amp;&amp; character &lt; 32) ||     // avoid shift
              (character &gt; 32 &amp;&amp; character &lt; 41) ||     // navigation keys
              character == 46) {                   // Delete Key (Add to these if you need)
              handleNonChar(character);            // function to handle non Characters
              nonChar = true;
          } else
              nonChar = false;
      } else {                                // This is keypress
          if (nonChar) return;                // Already Handled on keydown
          character = (evt.charCode) ?
                     evt.charCode : evt.keyCode;
          if (character &gt; 31 &amp;&amp; character &lt; 256)        // safari and opera
              handleChar(character);               //
      }
  }
  </code></pre>
  
  <p>The handler is called in the pages like this:</p>
  
  <pre><code>&lt;script&gt;
  document.onkeydown = function(e) {handleKeys(e)}
  document.onkeypress = function(e) {handleKeys(e)}
  var nonChar = false;
  &lt;/script&gt;
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Tim</p>