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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:29:55+00:00 2026-06-06T10:29:55+00:00

Here is my problem: http://testepi.kvalitne.cz/test/ I do not want the delay between a keypress

  • 0

Here is my problem: http://testepi.kvalitne.cz/test/

I do not want the delay between a keypress and the movement of the square. I would also like to know how to move diagonally (pressing two keys at same time).

My code:

$(function(){

document.addEventListener("keydown", move, false);

var x = 0;
var y = 0;

function move(event){
  if(event.keyCode==37){
        x -= 10;
  $("#square").css("left", x);
  }

  if(event.keyCode==39){
        x += 10;
  $("#square").css("left", x);
  }

  if(event.keyCode==38){
        y -= 10;
  $("#square").css("top", y);
  }

  if(event.keyCode==40){
        y += 10;
  $("#square").css("top", y);
  }

}

});
  • 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-06T10:29:57+00:00Added an answer on June 6, 2026 at 10:29 am

    First, to avoid the keypress/repeat delay, you have to wrap your program in a loop, and make the state of the keyboard available inside the scope of that loop, secondly to monitor multiple keypresses you need to keep track of individual keydown and keyup events:

    var x = 0;
    var y = 0;
    
    // store the current pressed keys in an array
    var keys = [];
    
    // if the pressed key is 38 (w) then keys[38] will be true
    // keys [38] will remain true untill the key is released (below)
    // the same is true for any other key, we can now detect multiple
    // keypresses
    $(document).keydown(function (e) {
        keys[e.keyCode] = true;
    });
    
    $(document).keyup(function (e) {
        delete keys[e.keyCode];
    });
    // we use this function to call our mainLoop function every 200ms
    // so we are not relying on the key events to move our square
    setInterval( mainLoop , 200 );
    
    function mainLoop() {
         // here we query the array for each keyCode and execute the required actions
         if(keys[37]){
            x -= 10;
            $("#square").css("left", x);
         }
    
         if(keys[39]){
            x += 10;
            $("#square").css("left", x);
         }
    
         if(keys[38]){
            y -= 10;
            $("#square").css("top", y);
         }
    
         if(keys[40]){
            y += 10;
            $("#square").css("top", y);
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem: http://jsfiddle.net/STG22/3/ I want that span would not split in two
Here is image of my problem: http://i48.tinypic.com/2roszr9.jpg . I want to ask you if
Here's a very quick example for my problem: http://img19.imageshack.us/img19/3575/workertool.png (I'm not allowed to post
Is this possible? you can se my problem here: http://granjalaaurora.com/test/test-jscrollpane.html I have this structure:
I made a little example of my problem here: http://peterbriers.be/test/float_html5.html As you can see,
Here is a live example of my problem http://jsfiddle.net/littlesandra88/xksB9/ I would have expected that
You can see the full problem here: http://users.cjb.net/syn4k/test.htm I have described the problem in
Here's my problem: http://jsfiddle.net/gregseth/548S2/ Why is there a white space between the elements? And
I've created an example of the problem here: http://jsfiddle.net/JustinN/qWeLT/1/ My actual code is hooked
I have the following problem: http://jsfiddle.net/DerNa/12/ Which is demonstrated more clearly the jump here:

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.