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

  • Home
  • SEARCH
  • 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 hierarchy: Case 1 RelativeLayout (screen size) | -> RelativeLayout (centered) | |->TextView
I am using EF 4.1 RC and CodeFirst/POCO to build my database by code.
Apologies if this is a stupid/easy question, but still getting used to everything in
I have server output that looks like this PLAYER_ENTERED name ipaddress username If the
There is probably a very simple answer to this, but I want to be
I am working on a asp.net mvc site that uses facebook social widgets. Whenever
I'm developing a WPF application using MVVM. Inside a Window, I have a control
I'm trying to learn .NET and WPF, and I'm making a calculator type application
I have some controls where I set their Name property to unique names, but
using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media;

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.