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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:02:03+00:00 2026-06-05T17:02:03+00:00

Working on a small game using an HTML5 canvas, and javascript. And it’s working

  • 0

Working on a small game using an HTML5 canvas, and javascript. And it’s working quite nicely, apart from keyboard keys occasionally getting “stuck”, ie. it’ll register a keydown event, but sometimes it won’t register the keyup event. This generally happens when hitting multiple keys at once, so it is in part a hardware or OS fault, but I’m hoping someone has ideas on how to work around this. The relevant parts of my code (written in coffeescript):

press = (direction) ->
    switch direction
        when 'W'
            game.keys.W = true
        when 'S'
            game.keys.S = true
        when 'A'
            game.keys.A = true
        when 'D'
            game.keys.D = true


release = (direction) ->
    switch direction
        when 'W'
            game.keys.W = false
        when 'S'
            game.keys.S = false
        when 'A'
            game.keys.A = false
        when 'D'
            game.keys.D = false

doKeyDown = (evt) ->
    switch evt.keyCode
        when 87 then press 'W'
        when 83 then press 'S'
        when 65 then press 'A'
        when 68 then press 'D'

doKeyUp = (evt) ->
    switch evt.keyCode
        when 87 then release 'W'
        when 83 then release 'S'
        when 65 then release 'A'
        when 68 then release 'D'

window.addEventListener('keydown', doKeyDown, false)

window.addEventListener('keyup', doKeyUp, false)

Basic stuff. It wouldn’t be a problem if javascript wasn’t entirely event-driven, but what can you do. My current idea for how to circumvent the problem would be to add a queue for keypresses, with a capacity for 2 or 3 keys, and when the limit is exceeded, the first key(s) in are removed and are switched to ‘false’. Still, it seems weird to add an artificial limit like this, when it should be avoidable!

So, the question is, is there a straightforward way to ensuring key states are saved accurately, without having to resort to workarounds? Preferably something that works independently of the framerate of the game (should work equally well whether it’s running at 1000 fps, or 3 fps).

  • 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-05T17:02:08+00:00Added an answer on June 5, 2026 at 5:02 pm

    Since keydown events fire rapidly, you could store a timestamp noting when the last time you heard the key was depressed:

    when 'A'
        game.keys.A = [true, +newDate()]
    

    Then, run an interval function to check for keys that are still registered as depressed but have not sent a keydown event for some duration (say, 1 second) and set them as released.

    My above example might require you to refactor a lot of your code (since game.keys.A is now an array instead of a boolean). Instead, you could hold the timestamps in their own object:

    last_keydown_times = {};
    ...
    
    when 'A'
        game.keys.A = true
        last_keydown_times['A'] = +new Date();
    

    Have the interval function check the members of last_keydown_times (perhaps in a for...in loop) and check for expired values:

    setInterval(function() {
        var now = +new Date();
        for(key in last_keydown_times) {
            if(now - last_keydown_times[key] >= 1000) {
                // run the release code for this key
            }
        }
    }, 1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small shooting game using cocos2d. It's working good in simulator.
I am working on a small game using C++, and I used Eclipse CDT's
I am working on a small game using webgl. Within this game I have
I am currently working on writing small game that is written in javascript, alot
I am working on a small game and game engine in C++ using DirectX.
I'm working on a small game in Flash (AS3) that generates levels using small
I'm working on a small roguelike game, and for any object/thing that is not
is there any small working program for recieving from and sending data to client
I've been working on a small game off and on related to words for
Suppose I am working on a card game, and I am using the numbers

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.