Niraj Pandkar
Niraj Pandkar

Reputation: 173

Incorrect keystroke dwell/hold time if typed fast

Aim: Gather data(dwell time, flight time, down to down time...) for keystroke dynamics.

Things accomplished: Successfully obtained the flight time between two keystrokes.

Problem: When I type fast, I am able to obtain flight time correctly but dwell time messes up.

Minimal, complete and verifiable example: https://jsfiddle.net/nirajpandkar/600orotn/

For example for reproducing the error, you could try typing the word "the" slowly, one word at a time. The dwell times are as follows -

Pressed key 84 for 0.107

Pressed key 72 for 0.091

Pressed key 69 for 0.091

Now try typing letters 't' and 'h' in the word "the" as fast as you can(you won't have to try hard). The dwell times are -

Pressed key 84 for 0.008

Pressed key 72 for 1490285526.868

Pressed key 69 for 0.074

Question: Why is this happening and how should it be fixed?

Upvotes: 2

Views: 346

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The reason for the error is that when typing fast it's possible for the keydown to fire before the previous keyup. Hence the dwellTime may not have been reset.

To alleviate the problem you could store the dwellTime on a per-keyCode basis so that it couples the keydown with the keyup for that given key, something like this:

var dwellTimes = {};
$('#inputbox').keydown(function(e) {
  if (!dwellTimes[e.which])
    dwellTimes[e.which] = new Date().getTime();
});

$('#inputbox').keyup(function(e) {
  var dwellTime = new Date().getTime() - dwellTimes[e.which];
  delete dwellTimes[e.which];

  $('#output').prepend("<p>Pressed key " + e.which + " for " + dwellTime / 1000 + "</p>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="inputbox" type='text'>
<div id="output"></div>

Note that I used prepend() in this example so you don't have to scroll down to see the newly raised events. Also, I changed the logic so that if a key is held down the dwellTime accounts for that, as your previous logic did not. If you don't need that behaviour you can simply remove the if condition from the keydown event handler.

Upvotes: 2

Related Questions