Skip to content

Instantly share code, notes, and snippets.

@DragorWW
Last active August 29, 2016 17:06
Show Gist options
  • Select an option

  • Save DragorWW/871358b99817959f3b79837136bb0343 to your computer and use it in GitHub Desktop.

Select an option

Save DragorWW/871358b99817959f3b79837136bb0343 to your computer and use it in GitHub Desktop.
JS key repeat event

Вот маленька песочница сделанная на основание стати из learn.javascript.ru.

Вот еще одна песочница от w3c.

KeyboardEvent . repeat

true if the key has been pressed in a sustained manner. Holding down a key MUST result in the repeating the events keydown, beforeinput, input in this order, at a rate determined by the system configuration. For mobile devices which have long-key-press behavior, the first key event with a repeat attribute value of true MUST serve as an indication of a long-key-press. The length of time that the key MUST be pressed in order to begin repeating is configuration-dependent.

Как работает

  • Windows Auto-repeat behavior is the same as in Gecko 4.0 and later.
  • Mac After the initial keydown event, only keypress events are sent until the keyup event occurs; the inter-spaced keydown events are not sent.
  • Linux The event behavior depends on the specific platform. It will either behave like Windows or Mac depending on what the native event model does.

Более подробное описание такого поведения есть тут

Библиотеки

Пример лога

Google chrome 52.0.2743.116 (64-bit) [mac]

keydown repeat=false keyCode=71 which=71 charCode=0 char=G
keypress repeat=false keyCode=103 which=103 charCode=103 char=g
--------------------------------------------------------------------------------
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keyup repeat=false keyCode=71 which=71 charCode=0 char=G

Firefox 47.0.1 [mac]

keydown repeat=false keyCode=71 which=71 charCode=0 char=G
keypress repeat=false keyCode=0 which=103 charCode=103 char=g
--------------------------------------------------------------------------------
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keyup repeat=false keyCode=71 which=71 charCode=0 char=G

Safari 9.1.1 (11601.6.17) [mac]

keydown repeat=undefined keyCode=71 which=71 charCode=0 char=G
keypress repeat=undefined keyCode=103 which=103 charCode=103 char=g
--------------------------------------------------------------------------------
keydown repeat=undefined keyCode=229 which=229 charCode=0 char=å
keydown repeat=undefined keyCode=229 which=229 charCode=0 char=å
keydown repeat=undefined keyCode=229 which=229 charCode=0 char=å
keydown repeat=undefined keyCode=229 which=229 charCode=0 char=å
keydown repeat=undefined keyCode=229 which=229 charCode=0 char=å
keydown repeat=undefined keyCode=229 which=229 charCode=0 char=å
keyup repeat=undefined keyCode=71 which=71 charCode=0 char=G

Opera 39.0.2256.48 [mac]

keydown repeat=false keyCode=71 which=71 charCode=0 char=G
keypress repeat=false keyCode=103 which=103 charCode=103 char=g
--------------------------------------------------------------------------------
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keydown repeat=true keyCode=71 which=71 charCode=0 char=G
keyup repeat=false keyCode=71 which=71 charCode=0 char=G

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment