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 8512057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:10:42+00:00 2026-06-11T04:10:42+00:00

Possible Duplicate: JavaScript move delay and multiple keystrokes I’m new and learning HTML5 canvas

  • 0

Possible Duplicate:
JavaScript move delay and multiple keystrokes

I’m new and learning HTML5 canvas together with javascript. I’ve created an event to move an object left and right, and my problem is the delay whenever you hold the key or switch to another key. I know there’s missing on my code below please help me. Thank you in advance.

c.addEventListener('keydown',this.check,true);
  function check(el) {
    var code = el.keyCode || el.which;
    if (code == 37 || code == 65){
    x -=1;
    }

    if (code == 39 || code == 68){
    x += 1;
    }

  el.preventDefault();
}
  • 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-11T04:10:43+00:00Added an answer on June 11, 2026 at 4:10 am

    Rather than trying to react directly to the keydown event, I’d suggest you use the keydown and keyup events to maintain a list of which keys are presently down. Then implement a “game loop” that checks every x milliseconds which keys are down and update the display accordingly.

    var keyState = {};    
    window.addEventListener('keydown',function(e){
        keyState[e.keyCode || e.which] = true;
    },true);    
    window.addEventListener('keyup',function(e){
        keyState[e.keyCode || e.which] = false;
    },true);
    
    x = 100;
    
    function gameLoop() {
        if (keyState[37] || keyState[65]){
            x -= 1;
        }    
        if (keyState[39] || keyState[68]){
            x += 1;
        }
    
        // redraw/reposition your object here
        // also redraw/animate any objects not controlled by the user
    
        setTimeout(gameLoop, 10);
    }    
    gameLoop();
    

    You’ll notice that this lets you handle multiple keys at once, e.g., if the user presses the left and up arrows together, and the problem of delay between subsequent keydown events when a key is held down goes away since all you really care about is whether a keyup has occurred.

    I realise you may not be implementing a game, but this “game loop” concept should work for you as shown in this very simple demo: http://jsfiddle.net/nnnnnn/gedk6/

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

Sidebar

Related Questions

Possible Duplicate: Javascript window resize event I want to get an event which is
Possible Duplicate: Javascript sleep How to delay execution in between the following in my
Possible Duplicate: JavaScript: remove event listener I am adding some listener in my html
Possible Duplicate: JavaScript open in a new window, not tab How can I open
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate
Possible Duplicate: JavaScript Variable inside string without concatenation - like PHP In PHP, double
Possible Duplicate: Javascript force open a link in a browser I am working on
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} AFAIK, there
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
Possible Duplicate: JavaScript: formatting number with exactly two decimals Can some one please help

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.