onKeyPress Vs. onKeyUp and onKeyDown

前端 未结 12 650
醉酒成梦
醉酒成梦 2020-11-22 07:44

What is the difference between these three events? Upon googling I found that:

  • The onKeyDown event is triggered when the user pre
12条回答
  •  渐次进展
    2020-11-22 08:06

    Just wanted to share a curiosity:

    when using the onkeydown event to activate a JS method, the charcode for that event is NOT the same as the one you get with onkeypress!

    For instance the numpad keys will return the same charcodes as the number keys above the letter keys when using onkeypress, but NOT when using onkeydown !

    Took me quite a few seconds to figure out why my script which checked for certain charcodes failed when using onkeydown!

    Demo: https://www.w3schools.com/code/tryit.asp?filename=FMMBXKZLP1MK

    and yes. I do know the definition of the methods are different.. but the thing that is very confusing is that in both methods the result of the event is retrieved using event.keyCode.. but they do not return the same value.. not a very declarative implementation.

提交回复
热议问题