在JavaScript中是否可以覆盖keydown重复延迟? [英] Is it possible to override the keydown repeat delay, in JavaScript?

查看:164
本文介绍了在JavaScript中是否可以覆盖keydown重复延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是手动设置保持键的重复率。

The objective is manually set a held key's "repeat rate".

例如,当在文本框中按住X键时,我明白了有浏览器特定的重复按下字符的方法。在某些情况下,它会暂停,然后不断触发按下的键。在其他情况下,它根本不重复。我想通过强制按下的键以特定的间隔重复来缓解这种情况,无论浏览器如何。

For example, when in a text box and pressing and holding the X key, I understand that there is browser-specific ways of repeating the pressed character. In some, it pauses, then continuously triggers the pressed key. In others, it doesn't repeat at all. I want to mitigate this by forcing the pressed key to repeat at a specific interval, regardless of browser.

通过研究,我想出了一个基于计时器的尝试,但在Safari中,它不会重复该字符。我有一个菜单系统,按住箭头滚动列表,但翻译动画和重复率彼此不一样。

Through research, I've come up with a timer-based attempt, but in Safari, it does not repeat the character. I've got a menu system where holding down arrow scrolls through the list, but the translation animation and repeat rate don't like each other.

var repeating = false;
var repeatRateTimer = null;

$( document ).bind( 'keyup', function( input ) {
    if( repeatRateTimer != null )
    {
        clearTimeout( repeatRateTimer );
        repeatRateTimer = null;
    }

    repeating = false;
} );

$( document ).bind( 'keydown', function( input ) {
    input.preventDefault( );

    if( repeating == true )
    {
        if( repeatRateTimer != null )
        {
            clearTimeout( repeatRateTimer );
            repeatRateTimer = null;
        }
        else
        {
            repeatRateTimer = setTimeout( function( ){ repeating = false; }, 1000 );
        }

        return;
    }
    repeating = true;

    // ...keyboard logic
} );

我可能搞砸了这一切......我试图重新创建这个SO帖子。但是,我觉得是更好的方法。有什么想法?

I may have botched this whole thing up...I tried to recreate a simplified version of this SO post. However, I feel there has to be a better way of doing this. Any thoughts?

更新:

我们可以假设最终用户没有将其OS键盘重复率设置为大于我想要使用的速率(1000ms)。如果是,那么它应该回到它们的重复率,因为它不会继续触发按键事件。如果不是(更可能是因为大多数人不修改它),那么我们将覆盖该行为以使其延迟我们指定的时间段。

We can assume that the end-user hasn't set their OS keyboard repeat rate greater than the rate I want to use (1000ms). If it is, then it should fall back to their repeat rate, since it won't keep triggering the key press event. If it isn't (more likely since most people don't modify that), then we would be overriding that behavior to make it delay our specified period.

推荐答案

好吧,我弄清楚为什么我的例子没有循环。在keydown循环中,它在超时之前清除了超时:

Well, I figured out why my example wasn't looping. In the keydown loop, it was clearing the timeout before it expired:

if( repeatRateTimer != null )
{
    clearTimeout( repeatRateTimer );
    repeatRateTimer = null;
}
else
{
    repeatRateTimer = setTimeout( function( ){ repeating = false; }, 1000 );
}

超时应该在到期后清除,所以 if 条件需要移入超时函数:

The timeout should be cleared only after it expires, so the if condition needed to be moved into the timeout function:

if( repeatRateTimer == null )
{
    repeatRateTimer = setTimeout( function( ) {
        repeating = false;
        clearTimeout( repeatRateTimer );
        repeatRateTimer = null;
    }, 1000 );
}

如果有人可以改进,我会保留这笔赏金,或者提供更好的选择。

I'll leave this bounty open in case someone can improve upon this, or provide a better alternative.

这篇关于在JavaScript中是否可以覆盖keydown重复延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆