Keyboard events (keydown/keyup/keypress)

Answer to "javascript listener, “keypress” doesn't detect backspace?" (http://stackoverflow.com/questions/4843472/javascript-listener-keypress-doesnt-detect-backspace/) Single keyclick -------------------- On Chrome: - keydown/keypress/keyup when browser registers a keyboard input (keypress is fired) - keydown/keyup if no keyboard input (tested with alt, shift, backspace, arrow keys) - keydown only for tab? On Firefox - keydown/keypress/keyup when browser registers a keyboard input (keypress is fired) and also for backspace, arrow keys, tab - keydown/keyup for alt, shift The JQuery which returns the charcode when this is not 0, otherwise it returns the keycode. Note that for digits 0,1,..,9 keycode=charcode but this is not true for non-numeric characters. Quote from https://api.jquery.com/keypress/ "Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms."

by Nikolay Petrov

HTML

Press key in the input box to see what events and codes are detected by your browser (<i>from Stackoverflow <a href="http://stackoverflow.com/questions/4843472/">javascript-listener-keypress-doesnt-detect-backspace</a></i>)
<p>
<input class="inputTxt" type="text" />
<div id="log"></div>

JavaScript

$("html").bind("keypress keyup keydown", function (event) {

    $("#log").html($("#log").html() + "<b>" + 
    event.type + "</b>" + " keycode: " + 
    event.keyCode + " charcode: " + 
    event.charCode + " which: " + 
    event.which + "<br>");
    
    return false;
});