Logging duration of keypress to end routine

Hi Nicolas,

Hmm, I will try and make the template active again. In my example I had the following routines:

before > start2 > after

before contains: a text element that lasts for one second and says “before text”

start2 contains: a keyboard component and a code snippet
The keyboard component is:

  1. set to accept ‘y’ and ‘n’ as responses
  2. NOT force the end of the routine
  3. Store all keys

The code snippet has the following in each tab:

  1. begin routine - window.kb = kb;
  2. each frame -
let keyEvents = kb.getEvents();
if (keyEvents .length > 0) {
    let keydownTimestamp;
    let keyupTimestamp;
    let i = 0;
    for (; i < keyEvents.length; i++) {
      if (keyEvents[i].pigletKey === 'space' && keyEvents[i].status === Symbol.for('KEY_DOWN')) {
        //console.log('found keydown at event ' + i);
        keydownTimestamp = keyEvents[i].timestamp;
       break;
      }
    }
    for (; i < keyEvents.length; i++) {
      if (keyEvents[i].pigletKey === 'space' && keyEvents[i].status === Symbol.for('KEY_UP')) {
        //console.log('found keyup at event ' + i);
        keyupTimestamp = keyEvents[i].timestamp;
        break;
      }
    }
    if (keydownTimestamp !== undefined && keyupTimestamp !== undefined) {
        console.log('duration ' + (keyupTimestamp - keydownTimestamp));
        kb.clearEvents();
        continueRoutine = false;
    }
}

After text contains: a text element that lasts for one second and says “after text”

This seems to only work once deployed in Pavlovia. Please let me know if this helps!

2 Likes