简体   繁体   English

如何获得释放按键的正确时间(JS的按键不正确)?

[英]How to get the correct timing of a key being released (JS' keyup is not accurate)?

When I use keyup I never get a timestamp later then about keydown+500ms even if I hold down the key much longer. 当我使用keyup时,即使我按住该键的时间更长,也永远不会在大约keydown + 500ms以后获得时间戳。 I'm using 我正在使用

 document.addEventListener('keyup', function(event) {
 if (event.code == 'KeyZ' )
  {
    var d = new Date();var time = d.getTime();
  }
 });

This is because the keydown will be fired multiple times when the key is held down. 这是因为按下键时会触发多次keydown Try placing a console.log in the keydown event handler and you'll see. 尝试将console.log放置在keydown事件处理程序中,您会看到。

This behaviour is not consistent between browsers and operating systems. 此行为在浏览器和操作系统之间不一致。 For a detailed list of behaviours, check out http://unixpapa.com/js/key.html . 有关行为的详细列表,请访问http://unixpapa.com/js/key.html

The timing between each keydown event when a key is held down can even be configured on Windows computers and possibly on other operating systems too. 每间定时keydown当一个键被按下的事件,甚至可以在Windows计算机上,并可能在其它操作系统上也配置。 See https://answers.microsoft.com/en-us/windows/forum/windows_xp-performance/can-you-disable-the-keyboard-autorepeat-function/ce1b29bc-544b-e011-8dfc-68b599b31bf5 请参阅https://answers.microsoft.com/zh-cn/windows/forum/windows_xp-performance/can-you-disable-the-keyboard-autorepeat-function/ce1b29bc-544b-e011-8dfc-68b599b31bf5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM