Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate time between key pressed

Tags:

jquery

I wanna calculate the time between key 37 and 39 pressed. This is the left and right key.

So the user would press the two keys and i must calculate between each 2 key presses how long each gap was.

like image 420
Harry Avatar asked Jul 01 '10 15:07

Harry


People also ask

How long is the average key press?

Typical times range from 70 to 150 milliseconds. Key digram durations are measured from the key-down event of one keypress to the key-down event of the following. These can range from 50 to 200 milliseconds.

How many keys can I press at once?

Most keyboards have a 6 key rollover which means they can correctly register that 6 keys are being pressed simultaneously. Some gaming computers have 10-25 key rollover capability! WOW!

How can I tell which key was pressed?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.


1 Answers

Something like:

var start = 0;
$("#input").keyup(function(e) {
    if(e.keyCode == 37) {
        start = new Date().getTime();
    } else if(e.keyCode == 39) {
        var elapsed = new Date().getTime() - start;
        alert("elapsed time in milliseconds is: " + elapsed);
        // start again
        start = 0;
    }
});

Mess with it here: http://jsfiddle.net/9vmb4/1/

like image 172
karim79 Avatar answered Sep 26 '22 08:09

karim79