JavaScript - Get all a symbol from keypress event

source: http://javascript.info/tutorial/keyboard-events#processing-the-character-keypress

by katalin_2003

HTML

<p>Open your console in the text field below:</p>
<input type="text" onkeydown="getChar('keypress')">

CSS

body {
  font-family: sans-serif;
}

JavaScript

// event.type must be keypress
function getChar(event) {
  if (event.which === null) {
    console.log(this.event.keyCode);
    console.log("IE");
    // return String.fromCharCode(event.keyCode) // IE
    return this.event.keyCode;
  } else if (event.which != 0 && event.charCode != 0) {
    console.log(this.event.keyCode);
    console.log("the rest");
    //return String.fromCharCode(event.which)   // the rest
    return this.event.keyCode;
  } else {
    console.log(this.event.keyCode);
    console.log("special key");
    return null // special key
  }
}