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

  • Home
  • SEARCH
  • 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 7409071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:01:11+00:00 2026-05-29T06:01:11+00:00

When user keeps a key pressed, I want the keydown() event get called only

  • 0

When user keeps a key pressed, I want the keydown() event get called only once, but its called until user stops pressing the key.

Here is what I am trying to do:

   $(document).keydown(function(event){
     var keycode = (event.keyCode ? event.keyCode : event.which);
     if(keycode == '39'){
              $("#box").animate({"left": "+=30px"}, "fast");
     } 
   });  

So, when user presses right arrow key, I want the div#box to move 30px to the right. It moves but if user keeps key pressed it flies away.

I need it to move only by 30px per press, and stop, even if user keeps the key pressed. Do you know how it can be accomplished?

  • 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-05-29T06:01:12+00:00Added an answer on May 29, 2026 at 6:01 am

    Keep track of which keys are down, and ignore keycode 39 until a keyup even clears it:

    var down = {};
    
    $(document).keydown(function(event){
         var keycode = (event.keyCode ? event.keyCode : event.which);
         if(keycode == '39'){
              if (down['39'] == null) { // first press
                  $("#box").animate({"left": "+=30px"}, "fast");
                  down['39'] = true; // record that the key's down
              }
         } 
       });
    
    $(document).keyup(function(event) {
         var keycode = (event.keyCode ? event.keyCode : event.which);
         down[keycode] = null;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When a user hits Refresh on their browser, it reloads the page but keeps
I have a table that keeps the user ratings for items. I want to
Simple question that keeps bugging me. Should I HTML encode user input right away
User equals untrustworthy. Never trust untrustworthy user's input. I get that. However, I am
I'm trying to configure GIT on my Windows XP machine, but SSH keeps creating
I have Bands and Codes and Users. the key/codes will let a user do
I need to make an animation which will accelerate when the user keeps a
I'm using JavaScript and Prototype and catching the key presses from the user. I
I want to have a database table that keeps data with revision history (like
I would like to distribute an application, but have license key that they can

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.