Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9038407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:28:57+00:00 2026-06-16T09:28:57+00:00

Using Javascript / jQuery, how can I get automatically repeating keydown events, or equivalent,

  • 0

Using Javascript / jQuery, how can I get automatically repeating keydown events, or equivalent, when someone holds down a key?

What I actually want is to be able to check whether a key is down, but from other questions here it looks like that’s not possible. The suggested workaround seems to be recording keydown and keyup events and then assuming the key is down if a keydown event has been recorded and no subsequent keyup.

That solution runs into a problem in my case. I am designing an online experiment. The user is supposed to hold down the “T” key for the entire experiment, never letting it up. The experiment consists of multiple trials and each trial has no access to information recorded by the previous trials. So, trial 1 could record keydown for T, but trial 2 wouldn’t have access to that record and thus wouldn’t know whether T was down or not.

Now, if holding down the T key would produce automatically repeating keydown events for T, I would have no problem because trial 2 would just catch the next keydown event for T to come along. But it looks like I don’t get automatically repeating keydown events from holding the key down, at least in Firefox. From what I can see it seems there is variation in the way different browsers handle holding a key down. What is a good cross-browser way to solve my problem?

By the way, if it matters, I also need to be able to detect keyup and keydown events for other keys while all this is going on.

EDIT: after reading some of the comments I went back and verified that I do indeed get repeating keydown events under ordinary circumstances. But I really don’t get them in the specific situation in which I need them. I’ve got some simple code which I think isolates the issue:

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
</head>

<body>
<div id="target"></div>
</body>

<script type="text/javascript">

var i;
function foo() {
    i++;
    $('#target').html(i);
}

function doTrial() { // do trial
    i=0;
    $(document).keydown(foo);
    $(document).keyup(endTrial);
}

function endTrial() { // end trial
    $('#target').html('');
    $(document).unbind('keydown',foo);
    $(document).unbind('keyup',endTrial);
    doTrial();
}

doTrial();

</script>

</html>

If you press a key and hold it down, then release, then press again, the behavior is as expected, i.e. there is a counter which increments while the key is held down, disappears when it’s released, and then starts incrementing again when it’s pressed again.

But if you press TWO keys down, then release ONE, I would have thought that the other (not released) key would continue sending keydown events so that the counter would (after resetting) continue incrementing. In fact, that doesn’t happen. Any idea why and how to make it happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:28:58+00:00Added an answer on June 16, 2026 at 9:28 am

    In the browsers I tried this in, I got repeated keydown events when holding a typeable key down. I don’t know if this is a problem you actually need to solve.

    But, if you did think you need to solve it OR if you want to control the repeat-rate yourself, you could do it like this:

    1. Capture the events for keydown and keyup.
    2. On keydown, set an interval timer that fires however often you want to know that the key is still down.
    3. On keyup for that key, stop the interval timer.
    4. You will get repeated notification in a cross browser way as long as the key is held down.

    Working demo here: http://jsfiddle.net/jfriend00/XbZYs/

    var downTimer;
    var lastKey;
    $(document.body).keydown(function(e) {
        // if not still the same key, stop the timer
        if (e.which !== lastKey) {
            if (downTimer) {
                clearInterval(downTimer);
                downTimer = null;
            }
        }
        // remember previous key
        lastKey = e.which;
        if (!downTimer) {
            // start timer
            downTimer = setInterval(function() {
                $("#result").append("+");
            }, 125);
        }
    }).keyup(function(e) {
        // stop timer
        if (downTimer) {
            clearInterval(downTimer);
            downTimer = null;
            lastKey = 0;
        }
    });
    

    If you want a key to auto-repeat forever until it is raised, even if other keys are pressed and released in the meantime and you want those other keys to do their own auto-repeating, then the OS does not ipmlement that behavior so you would have to implement it yourself. You can do something like this which calls a callback function for every key repeat event:

    Working demo: http://jsfiddle.net/jfriend00/aD3Eg/

    // this is called for every manufactured repeat event
    // the frequency of the repeat event is determined by the time value set
    // on setInterval() below
    function repeatCallback(key) {
        $("#result").append(key + " ");
    }
    
    var repeatState = {};
    $(document.body).keydown(function(e) {
        var key = e.which;
        // if no time yet for this key, then start one
        if (!repeatState[key]) {
            // make copy of key code because `e` gets reused
            // by other events in IE so it won't be preserved
            repeatState[key] = setInterval(function() {
                repeatCallback(key);
            }, 125);
        } else {
            // nothing really to do here
            // The key was pressed, but there is already a timer
            // firing for it
        }
    }).keyup(function(e) {
        // if we have a timer for this key, then stop it 
        // and delete it from the repeatState object
        var key = e.which;
        var timer = repeatState[key];
        if (timer) {
            clearInterval(timer);
            delete repeatState[key];
        }
    });
    ​
    

    The repeatCallback function is called for all of these manufactured auto-repeat events and passed the key that is auto-repeating. ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a drop down menu using javascript/jquery but can't get my
I wanna know how i can get the opacity param using javascript / jQuery.
How can we get the selected value of PrimeFaces <p:selectOneMenu> using JavaScript/jQuery? I am
How can I manipulate using JavaScript (JQuery) a site, not in my server, that
Possible Duplicate: Prevent any form of page refresh using jQuery/Javascript how can i prevent
I was creating a countdown timer using javascript; I can use jQuery. I want
How can you copy text to clipboard using Javascript (or even nicer a jQuery
In a htmlpage using jQuery or JavaScript how can the following be achieved? User
Is it possible to get the top position of an element using javascript/jquery ?
I am using Javascript-JQuery to submit 2 aspx pages, $('#MyForm1').submit(); $('#MyForm2').submit(); for some reason

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.