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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:03:45+00:00 2026-06-02T19:03:45+00:00

is it possible to have to use two keys for example and it will

  • 0

is it possible to have to use two keys for example and it will accumulate the time pressed for each key, for example key LEFT and RIGHT, so every time you click on one of them it will accumulate the time it has been pressed for, in case you press the same key again, it will add the time so at the end you get total time pressed for each key.

How can this be done?

  • 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-02T19:03:46+00:00Added an answer on June 2, 2026 at 7:03 pm

    Depends quite a bit on the platform. But event-driven platforms have it kinda easy, if they treat key-down and key-up as events (most do, including .net, java, and Windows).

    Just listen for the key-down event, and record the time it happened. When you get a key-up event for that same key, just subtract the time it happened from the time you recorded. The difference between the two will be the duration of that keypress.

    Catch is, depending on how your platform handles key repeat, you may get multiple key-downs for the same key before you get a key-up. Only keep track of the first (since the last key-up or app start, of course) for each key. And since you can press more than one key at a time, if you want to do this right, you’ll be able to remember the key-down time for multiple keys.

    If you want to keep track of the total time, then take that time difference you found on key-up, and add it to a running total for that key.

    In JS:

    (function() {
        var output = document.getElementById('output'),
            totalTime = {},
            pressed = {};
    
        window.onkeydown = function(e) {
            if (e.which in pressed) return;
            pressed[e.which] = e.timeStamp;
        };
    
        window.onkeyup = function(e) {
            if (!(e.which in pressed)) return;
            var duration = ( e.timeStamp - pressed[e.which] ) / 1000;
            if (!(e.which in totalTime)) totalTime[e.which] = 0;
            totalTime[e.which] += duration;
            output.innerHTML +=
                '<p>Key ' + e.which + ' was pressed for ' +
                duration + ' seconds this time' +
                '(' + totalTime[e.which] + ' total)</p>';
            delete pressed[e.which];
        };
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question in RoR: Is it possible to have two fields use foreign keys
first time use JTree. Just wondering is it possible to have more than one
I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
Is it possible to have Rhino use a newer implementation of JS than 1.7?
Is it possible to have an Applet use a local jar? My Applet application
Does anyone know if it's possible to have a bundle use the annotation reader
Quick question, is it possible to have an input field in a form use
Possible Duplicate: How to catch exceptions I have not really had to use try
Is it possible to use a public repository like github and have a branch
I have a similar question as here: Is it possible to use ContentProvider and

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.