2

如何检测:按两次相同的键,但第二次按住 2 秒?

我正在寻找一种有效的方法来检测键码 37(左箭头)(或必要时的任何其他键)的键按下事件,然后在释放键后 1.5 或 2 秒内再次按下相同键码,在给定的延迟,例如 1 秒。

这是返回而不混淆“返回,在上一页”(这是我想要做的)和“在这个元素之前的元素,在同一页上”

我已经尝试在 $(document.ready() 标记中使用 old_keycode 和 old_keycode_timepressed 和 new_keycode new_keycode_timepressed,但是我需要为我想要的每个键码复制/粘贴整个代码,并且代码本身效率不高。

Anyone have heard of a plugin that can help me with that, or anyone is willing to help me whit it?

Thanks for your time.

4

1 回答 1

2

jsFiddle 演示

var twice_37 = 0;

$(document).on('keyup', function( e ){

  if(e.which===37){         // If left arrow    

    if(twice_37===1){       // (remember that twice_37 is 0 initially)
      alert('Do something! (you pressed Left twice!)');
    }

    twice_37 = 1;          // Set to 1 and...
    setTimeout(function(){ // ...reset to 0 after 1s
      twice_37 = 0; 
    }, 1000);

  }

});
于 2013-05-09T04:45:18.343 回答