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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:31:58+00:00 2026-06-15T01:31:58+00:00

I know there’s this keypress even on Window , which repeats if you hold

  • 0

I know there’s this keypress even on Window, which repeats if you hold the key, but it has this slight pause before the repetition starts. I don’t want this pause. I’m writing a game where I want the player to react immediately, not after a pause.

What’s the preferred way to handle keyboard events for web based games?

  • 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-15T01:31:59+00:00Added an answer on June 15, 2026 at 1:31 am

    Listen for “keydown”, instead of “keypress”.
    Keydown fires constantly until you let go.

    BUT:

    Do not just do something like:

    window.addEventListener("keydown", function (evt) {
        if (evt.keyCode === 32) { player.fire(); }
    });
    

    You’ll end up with some people firing 15 times a second, and some people firing 60 times a second, and maybe even some firing 120 times per second.

    Instead, have a Keyboard object which updates itself any time n event fires:

    var Keyboard = {
        keys : {},
        keyPress : function (evt) {
            if (this.keys[evt.keyCode] > 0) { return; }
            this.keys[evt.keyCode] = evt.timeStamp || (new Date()).getTime();
        },
        keyRelease : function (evt) {
            this.keys[evt.keyCode] = 0;
        }
    };
    window.addEventListener("keydown", Keyboard.keyPress.bind(Keyboard));
    window.addEventListener("keyup", Keyboard.keyRelease.bind(Keyboard));
    

    Then, during your update cycle, have the character check the Keyboard for the key it wants.
    As an added bonus, they key has a timestamp of when it was first pressed, in case you want to add charge-up shots, or holding the button to jump higher.

    Your units should then keep track of things like how often they’re allowed to fire, and when the last time was, inside of their own update area.

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

Sidebar

Related Questions

I know there is a lot on this topic but I can't get any
I know there are a lot of questions around on this subject, but I've
I know there are several question like this one but I feel I've spent
I know there is a question about this subject already but I think it's
I know there are similar post to this but I couldn't find my answer
I know there are several topics out there about this, but none of them
I know there has been similar questions before, but I can't find a solution
I know there are dozens of questions that ask about this error, but none
I know there are actually a number of questions similar to this one, but
I know there are a lot of questions around this subject, but I cannot

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.