如何检测:按两次相同的按键,但第二次按住2秒钟 [英] How to detect: Press twice the same key, but hold it the second time for 2 seconds
问题描述
如何检测:按两次相同的键,但第二次按住2秒?
How to detect: Press twice the same key, but hold it the second time for 2 seconds ?
我正在寻找一种检测keydown的有效方法键码37(左箭头)(或必要时的任何其他键)的事件,然后在释放键之后,在给定的延迟(如1秒)内,在相同的键码的第二次键控1.5或2秒内。
I'm looking for an efficient way to detect a keydown event for the keycode 37 (left arrow) (or any other key when necessary) and then a second keydown of the same keycode for 1.5 or 2 seconds just after releasing the key, Within a given delay, like 1 second.
这是回过头来看,不要混淆回去,在上一页上(这是我想要做的)和在这个之前继续元素,在同一个元素上页面
This is to go back without confusing between "Go back, on the previous page" (this is what i want to do) and "Go on the element before this one, on the same page"
我已经在$(document.ready()标记中尝试使用old_keycode和old_keycode_timepressed以及new_keycode new_keycode_timepressed,但我需要复制/粘贴每个代码的完整代码我想要的密钥代码,代码本身效率不高。
I already tried with old_keycode and old_keycode_timepressed and new_keycode new_keycode_timepressed in the $(document.ready() tag, but I need to copy/paste the entire code for each keycode i want, and the code itself isn't efficient.
任何人都听说过一个可以帮助我的插件,或者有人愿意帮助我?
Anyone have heard of a plugin that can help me with that, or anyone is willing to help me whit it?
感谢您的时间。
推荐答案
jsFiddle demo
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);
}
});
这篇关于如何检测:按两次相同的按键,但第二次按住2秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!