简体   繁体   English

当按住箭头键时,如何在Firefox中获得自动重复的keydown事件?

[英]How can I get auto-repeated keydown events in Firefox when arrow keys are held down?

I've got several editable div s. 我有几个可编辑的div I want to jump through them by pressing arrow keys (38 and 40). 我想按箭头键(38和40)跳过它们。

Firefox 3 on Mac OS and Linux won't repeat the events on holding the key. Mac OS和Linux上的Firefox 3不会重复持有密钥的事件。 Obviously only keypress events are supported for repetition. 显然,只有keypress事件才能重复。 As the keys 38 and 40 are only supported on keydown I'm kind of stuck. 因为键38和40仅在keydown支持,所以我有点卡住了。

Yes, you're kind of stuck. 是的,你有点卡住了。 You could emulate the behaviour you want by using timers until you receive the corresponding keyup , but this obviously won't use the user's computer's keyboard repeat settings. 你可以模仿你想使用定时器行为,直到收到相应的keyup ,但这显然不会使用用户的电脑的键盘重复设置。

The following code uses the above method. 以下代码使用上述方法。 The code you want to handle keydown events (both real and simulated) should go in handleKeyDown : 你想要处理keydown事件(真实和模拟)的代码应该在handleKeyDown

var keyDownTimers = {};
var keyIsDown = {};
var firstKeyRepeatDelay = 1000;
var keyRepeatInterval = 100;

function handleKeyDown(keyCode) {
    if (keyCode == 38) {
        alert("Up");
    }
}

function simpleKeyDown(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode;
    handleKeyDown(keyCode);
}

document.onkeydown = function(evt) {
    var timer, fireKeyDown;
    evt = evt || window.event;
    var keyCode = evt.keyCode;

    if ( keyIsDown[keyCode] ) {
        // Key is already down, so repeating key events are supported by the browser
        timer = keyDownTimers[keyCode];
        if (timer) {
            window.clearTimeout(timer);
        }

        keyIsDown[keyCode] = true;
        handleKeyDown(keyCode);

        // No need for the complicated stuff, so remove it
        document.onkeydown = simpleKeyDown;
        document.onkeyup = null;
    } else {
        // Key is not down, so set up timer
        fireKeyDown = function() {
            // Set up next keydown timer
            keyDownTimers[keyCode] = window.setTimeout(fireKeyDown, keyRepeatInterval);
            handleKeyDown(keyCode);
        };

        keyDownTimers[keyCode] = window.setTimeout(fireKeyDown, firstKeyRepeatDelay);
        keyIsDown[keyCode] = true;
    }
};

document.onkeyup = function(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode;
    var timer = keyDownTimers[keyCode];
    if (timer) {
        window.clearTimeout(timer);
    }
    keyIsDown[keyCode] = false;
};

You can use keypress and check the e.keyCode == 38,40 instead of e.which or e.charCode This is consistent across Mac and Win. 您可以使用按键并检查e.keyCode == 38,40而不是e.which或e.charCode这在Mac和Win中是一致的。

$('#test').bind($.browser.mozilla ? 'keypress' : 'keyup', function(e) {
    if ( (e.which || e.keyCode) == 40 ) { /* doSometing() */ }
});

See JavaScript Madness: Keyboard Events (3.2. Values Returned on Character Events) and event.keyCode on MDC. 请参阅JavaScript疯狂:键盘事件 (3.2。在字符事件上返回的值)和MDC上的event.keyCode。

暂无
暂无

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

相关问题 使用 JavaScript 每次按下键时,即使它被按住,我如何才能只获取一次 keydown 事件? - How can I get keydown events only once each time the key is pressed, even if it's held down, with JavaScript? 按住多个键时未触发JavaScript KeyDown事件 - JavaScript KeyDown event not triggered when multiple keys are held down 跨浏览器的方式,在按住键时自动重复重复按下事件 - Cross-browser way to get automatically repeating keydown events when key is held down Javascript:限制按下按键事件监听器在按下时可以多快调用一次函数 - Javascript: Limit how quickly a keydown event listener can call a function when held down Angular:使用方向键上下导航时,如何防止滚动条移动? - Angular: How can I prevent the scrollbar from moving when navigating with the arrow keys up and down? Svelte:按向上箭头和向下箭头键时,如何将焦点设置到列表项中的上一个/下一个元素? - Svelte: How can I set the focus to the previous/next element in the list item when pressing UP arrow and DOWN arrow keys? 按住按键时忽略重复的键盘事件 - Ignore repeated keyboard events when holding down the keys HTML Canvas:按住键时控制键按下注册表 - HTML Canvas: Controlling keydown registry when key is held down 在 KnockoutJs 中为箭头键使用 keydown 事件时,其他键不起作用 - Other keys not working when using keydown event for arrow keys in KnockoutJs 在玩游戏时,如何阻止我的网站通过箭头键上下滚动? - How do I stop my website from scrolling up and down with the arrow keys when playing games?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM