onKeyPress Vs. onKeyUp and onKeyDown

前端 未结 12 658
醉酒成梦
醉酒成梦 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

    Updated Answer:

    KeyDown

    • Fires multiple times when you hold keys down.
    • Fires meta key.

    KeyPress

    • Fires multiple times when you hold keys down.
    • Does not fire meta keys.

    KeyUp

    • Fires once at the end when you release key.
    • Fires meta key.

    This is the behavior in both addEventListener and jQuery.

    https://jsbin.com/vebaholamu/1/edit?js,console,output <-- try example

    (answer has been edited with correct response, screenshot & example)

    0 讨论(0)
  • 2020-11-22 08:06

    A few practical facts that might be useful to decide which event to handle (run the script below and focus on the input box):

    $('input').on('keyup keydown keypress',e=>console.log(e.type, e.keyCode, e.which, e.key))
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input/>

    Pressing:

    • non inserting/typing keys (e.g. Shift, Ctrl) will not trigger a keypress. Press Ctrl and release it:

      keydown 17 17 Control

      keyup 17 17 Control

    • keys from keyboards that apply characters transformations to other characters may lead to Dead and duplicate "keys" (e.g. ~, ´) on keydown. Press ´ and release it in order to display a double ´´:

      keydown 192 192 Dead

      keydown 192 192 ´´

      keypress 180 180 ´

      keypress 180 180 ´

      keyup 192 192 Dead

    Additionally, non typing inputs (e.g. ranged <input type="range">) will still trigger all keyup, keydown and keypress events according to the pressed keys.

    0 讨论(0)
  • 2020-11-22 08:07

    onkeydown is fired when the key is down (like in shortcuts; for example, in Ctrl+A, Ctrl is held 'down'.

    onkeyup is fired when the key is released (including modifier/etc keys)

    onkeypress is fired as a combination of onkeydown and onkeyup, or depending on keyboard repeat (when onkeyup isn't fired). (this repeat behaviour is something that I haven't tested. If you do test, add a comment!)

    textInput (webkit only) is fired when some text is entered (for example, Shift+A would enter uppercase 'A', but Ctrl+A would select text and not enter any text input. In that case, all other events are fired)

    0 讨论(0)
  • 2020-11-22 08:07

    Basically, these events act differently on different browser type and version, I created a little jsBin test and you can check the console for find out how these events behavior for your targeted environment, hope this help. http://jsbin.com/zipivadu/10/edit

    0 讨论(0)
  • 2020-11-22 08:15

    Check here for the archived link originally used in this answer.

    From that link:

    In theory, the onKeyDown and onKeyUp events represent keys being pressed or released, while the onKeyPress event represents a character being typed. The implementation of the theory is not same in all browsers.

    0 讨论(0)
  • 2020-11-22 08:15

    The onkeypress event works for all the keys except ALT, CTRL, SHIFT, ESC in all browsers where as onkeydown event works for all keys. Means onkeydown event captures all the keys.

    0 讨论(0)
提交回复
热议问题