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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:50:27+00:00 2026-05-23T09:50:27+00:00

Imagine this code: if (navigator.appName == Opera) document.onkeypress = function (e) { console.log(e.keyCode); };

  • 0

Imagine this code:

if (navigator.appName == "Opera")
    document.onkeypress = function (e) { console.log(e.keyCode); };
else 
    document.onkeydown = function (e) { console.log(e.keyCode); };

What it does is pretty obvious, I guess. The problem is, if you hold the key for a long period of time, it will be registered a lot of times. In my app, this is a problem because it makes my app do a lot of unnecessary computing. Is it possible to somehow only get a keypress once, but with info on how long the key was held?

  • 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-05-23T09:50:27+00:00Added an answer on May 23, 2026 at 9:50 am

    Here you go:

    var pressed = {};
    
    window.onkeydown = function(e) {
        if ( pressed[e.which] ) return;
        pressed[e.which] = e.timeStamp;
    };
    
    window.onkeyup = function(e) {
        if ( !pressed[e.which] ) return;
        var duration = ( e.timeStamp - pressed[e.which] ) / 1000;
        // Key "e.which" was pressed for "duration" seconds
        pressed[e.which] = 0;
    };
    

    Live demo: http://jsfiddle.net/EeXVX/1/show/

    (remove the “show/” part of the URL to view the code for the demo)

    So you have the pressed object which monitors which keys are currently being pressed and at what point (in time) they have been pressed.

    Inside the keyup handler, you determine if the key was being pressed, and if so, calculate the duration by subtracting the time-stamps of the keyup/keydown events.

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

Sidebar

Related Questions

Imagine this small code $(document).ready(function() { $(#loadingDiv).load(sub/__window_div.php); }) It runs okay in Chrome, Firefox,
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>
Imagine this function: void SoundManager::playSource(ALuint sourceID, float offset) { alSourceStop(sourceID); ALint iTotal = 0;
Consider this code snippet: transaction.executeSql( 'SELECT * FROM pv_master;', [], function(transaction, result) { if(result.rows.length
imagine this code for (int iDay = 1; iDay <= total_days; iDay++) { question
A question about c++ design and efficiency... Imagine this code - Database db; class
I have a challenge: Imagine you have a set of messages like this: Code
Imagine this code: int foo() { return foo(); } The compiler can optimize this.
Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I'm coding

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.