Search code examples
javascriptfirefoxtimestampkeydownkeyup

Firefox keyboard events have incorrect timeStamp values


I have some software that captures the times for each keystroke. I am using the keyup and keydown events to get both the up and down times. These events have a parameter, timeStamp, which should be milliseconds relative to the epoch. On Firefox, this is milliseconds, but it is way too small to be since the epoch. This is working correctly for me on Chrome and Safari.

Here's a snippet of the code I'm using:

function keyDownHandler(event) {
    var key = event.which,
        when = event.timeStamp;
    ...
}   

function keyUpHandler(event) {
    var key = event.which,
        when = event.timeStamp;
    ...
}   

$(element).keydown(keyDownHandler);
$(element).keyup(keyUpHandler);

Am I missing something here? An easy way to reproduce is to look at JQuery's page for keyup and type in their demo. With Chrome and Safari, the timeStamp value returned is 1446582863442, but in Firefox it's 2444770694.


Solution

  • This is an open issue from 2004, and affects other events too. The issue is that Firefox is using a different epoch time; rather than the unix Epoch time, it seems to be using the system start time.

    From the W3 definition of the timeStamp attribute:

    Due to the fact that some systems may not provide this information the value of timeStamp may be not available for all events. When not available, a value of 0 will be returned. Examples of epoch time are the time of the system start or 0:0:0 UTC 1st January 1970.