Measuring key lift time relative to stimulus onset

Hi @Becca ,

This custom code was really helpful - thanks! However, I’m still having trouble coding key durations in my experiment in local PsychoPy, let alone in Pavlovia.

Where my needs differ from the code you sent is that my participants will type a whole word as opposed to just a letter. I want the key duration for each key pressed in that word (6 keys/letters) to be saved to a list which then appears in my .xlsx file as a list of 6 key durations for that word stimulus.

I am so stuck with this and would be incredibly grateful for any assistance! Here is my code:

Begin Routine

# a keyboard object (must differ from object used online)
mykb = keyboard.Keyboard()
#numPresses = 10

# which keys are we watching? 
keysWatched=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z','backspace','return','space',',','.','/',
'`',';','#','[',']','1','2','3','4','5','6','7','8','9','0','-','=','rshift',
'lshift']

# what are the assumed key statuses at the start of the routine
status =['up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up',
'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 
'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 
'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up', 'up']

# how many keyPresses have been counted so far
keyCount = 0 
statusList = []

pressTime = 0
keyPressTime = []
liftTime = 0 
keyPressDuration = []

Each Frame

# poll the keyboard
keys = mykb.getKeys(keysWatched, waitRelease = False, clear = False)

if len(keys):# if a key has been pressed
    for i, key in enumerate(keysWatched):
        if keys[-1].name == key:
            if keys[-1].duration:
                status[i] = 'up'
                statusList.append('up')
            else:
                status[i] = 'down'
                statusList.append('down')
                
#get times:
if len(statusList)>1:
    if statusList [-1] != statusList[-2]:# the last 2 key events were different
        if statusList[-1] =='down':# this was a press event
            pressTime = taskClock.getTime()
            keyPressTime.append(pressTime)
        elif statusList[-1] =='up':
            liftTime = taskClock.getTime() - pressTime #key press duration, (i.e. 
            # difference between press and release)
            keyPressDuration.append(liftTime)
            #continueRoutine = False

As you can see this is incredibly similar to the code you and Thomas provided, with the main addition of the .append code in the ‘Each Frame’ section I have added.

End Routine

thisExp.addData('keyPressTime', keyPressTime)
thisExp.addData('keyPressDuration', keyPressDuration)
thisExp.addData('statusList', statusList)

The issue is that in the .xlsx file I get this:
image

Why are keyPressTime and keyPressDuration empty?

Many thanks for any ideas!