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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:07:29+00:00 2026-06-03T05:07:29+00:00

Situation : I have a keydown handler with a switch for what key is

  • 0

Situation: I have a keydown handler with a switch for what key is pressed, fairly standard stuff, but when any key is held down the keydown event fires repeatedly (rather than just once when the key is actually pressed).

Why it’s a problem: I want to keep the keydown listener active, i.e. be able to detect more than one key being pressed at once, but only have the event fire once per keydown. I will want to do something on the keyup for that keyCode based on the time between down and up, but that timing is getting screwed up because of the multiple firings.

What I’ve tried: I am currently keeping a list of keyCodes that are down, and checking against those in my keydown handler to keep the default behaviour from happening if the keyCode is in my list. However, the event is still firing very often and I’m concerned about the efficiency/elegance of this solution.

The actual question: Is there a good way to limit firing of the keydown event to just when the key is physically pushed down, or to only listen for specific keyCodes?

  • 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-03T05:07:30+00:00Added an answer on June 3, 2026 at 5:07 am

    In the keydown handler, check whether the key is the same as the last key handled. If it was, exit early. On keyup, forget the last key handled.

    For example:

    var lastEvent;
    var heldKeys = {};
    
    window.onkeydown = function(event) {
        if (lastEvent && lastEvent.keyCode == event.keyCode) {
            return;
        }
        lastEvent = event;
        heldKeys[event.keyCode] = true;
    };
    
    window.onkeyup = function(event) {
        lastEvent = null;
        delete heldKeys[event.keyCode];
    };​
    

    Demo here (click on the “Result” panel and hit keys).

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

Sidebar

Related Questions

I have situation where I am mapping table's columns to the primary key of
I have a situation where, upon loading my swf, I add an event listener
Situation I have a fairly simple app with 2 layouts using SharedPreferences. Main.xml Settings.xml
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
Situation: I have some persons with certain skills and they can/might belong to more
Situation I have been working on a project lately where the UI development seems
Situation I have a client library that uses the Windows Azure AppFabric Service Bus
Situation I have a QList<QVariant> where each QVariant item should be a QList<QVariant> of
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation, where running a query that filters by an indexed column in

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.