1
$(window).keypress(function(evt) {
  console.log(evt.which)//output is 97 when pressed a   
})

But

$(window).keyup(function(evt) {
  console.log(evt.which) //output is 65 when pressed a  
})

My question is why they are giving different output

Roshan
  • 2,144
  • 2
  • 16
  • 28
  • +1 Interested to see the reason! – Rob Schmuecker Aug 08 '14 at 14:10
  • http://stackoverflow.com/questions/11030532/keypress-and-keyup-why-is-the-keycode-different – Jamie Dunstan Aug 08 '14 at 14:16
  • You have 3 specific keyboard event handlers... onkeypress, onkeydown, and onkeyup. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers. – amazedinc Aug 08 '14 at 14:16
  • onkeypress gives you the character code corresponding to the character typed, as opposed to the key code of the key pressed (as it is in keyup and keydown). – amazedinc Aug 08 '14 at 14:17
  • In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. Link: http://unixpapa.com/js/key.html – amazedinc Aug 08 '14 at 14:17

0 Answers0