keypress and keyup - why is the keyCode different?

后端 未结 3 2014
闹比i
闹比i 2020-11-28 07:00

Related: JavaScript KeyCode vs CharCode

Here is some code you can try at home or in a jsfiddle:

el.addEventListener( \'keyup\', function( e ) { 
             


        
3条回答
  •  -上瘾入骨i
    2020-11-28 07:23

    Well, I stumbled upon one difference when i was trying to copy user's entry from one input of the form to some other part of the form , which I had locked for my for users to edit. What i found was, that whenever a user moved to the next label using key upon completing the input, one last keyboard entry was missed in the copied entry when I used eventListener to keypress and this got resolved on using keyup.

    So, in conclusion Keypress listens to the state at the instant when the key was pressed, leaving aside the result of keypress, whereas keyup listens to the system status after the key has been pressed and includes the result of the keypress.

提交回复
热议问题