3

I found that keypress is not returning the correct charCode value. http://jsfiddle.net/cheonhyang/t9hL1k1n/

HTML:

<h1> keydown, keyup and keypress event</h1>
<input id="input"></input>

JS:

var input = document.getElementById('input');
input.addEventListener('keydown', function(e){
    console.log("input keydown");
     console.log("which: " + e.which);
    console.log("keyCode: " + e.keyCode);
    console.log("charCode: " + e.charCode);

});
input.addEventListener('keyup', function(e){
    console.log("input keyup");
     console.log("which: " + e.which);
    console.log("keyCode: " + e.keyCode);
    console.log("charCode: " + e.charCode);
});
input.addEventListener('keypress', function(e){
    console.log("input keypress");
     console.log("which: " + e.which);
    console.log("keyCode: " + e.keyCode);
    console.log("charCode: " + e.charCode);
});

When pressed "a", the charCode is returning "A", while when pressed "A", the charCode is returning "a".

It's very weird.

When typing "a", the result will be :

input keydown
which: 65
keyCode: 65
charCode: 0

input keypress
which: 97
keyCode: 97
charCode: 97

input keyup
which: 65
keyCode: 65
charCode: 0

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tianxiang Zhang
  • 162
  • 2
  • 13
  • I think I made a mistake about the ASCII code in the question. – Tianxiang Zhang Aug 05 '15 at 15:28
  • Thanks for the reference. The answer for this question would be : keypress always return value for the actual typed character, while keyup and keydown always returns the key so no matter it's a "a" or "A", it always returning 65 which is the ASCII code for "A". – Tianxiang Zhang Aug 05 '15 at 15:30

0 Answers0