Search code examples
pythonkeypresslogfilepsychopyresponse-time

psychopy logging time when participant stopped moving cursor


I am using a rating scale. Participants use the 't' and 'b' keys to move the cursor along the scale. Each trial is currently 6 seconds long. If a participant stops pressing 't' or 'b' before 6 seconds are up, I want to log the time of the last keypress in my logfile. However, I'm not sure how to check which keypress is the last. I was thinking of logging the RT of the last keypress in the list, but code is checking for keypresses on every refresh. This is what I have so far:

trialNum=0
for eachPic in catPictures:
    prevPos = 0
    key=[]
    b_list=[]
    t_list=[]
    timer = core.CountdownTimer(TrialDuration)
    event.clearEvents() # get rid of other, unprocessed events
    while timer.getTime() > 0:
    for key in event.getKeys():
        if key in ['escape']:
            core.quit() # quit if they press escape
        if key in ['b']:
            # add keypress to list for each keypress. then move cursor proportionally to length of this list
            b_list.append(key)
            prevPos+=len(b_list)
        if key in ['t']:
            t_list.append(key)
            prevPos-=len(t_list)

Solution

    1. I would just have one list of keys and check the last element once the timer is up, i.e. after the while-loop (upon finish trial).
    2. Don't initiate a whole new timer in each loop. Just reset it. Much more ressource-efficient.
    3. Indent stuff in the while loop.
    4. I don't understand why you move the cursor the distance of the number of previous key presses in that trial. It seems more reasonable to move it a fixed distance per key press. So I did that below.
    5. Definitely check out Jeremy Gray's proposal of using the built-in psychopy.visual.RatingScale (another answer to this question).

    Untested code:

    timer = core.CountdownTimer(TrialDuration)
    stepSize = 1
    for eachPic in catPictures:
        prevPos = 0  # keeps track of the slider position
        rts=[]  # used to keep track of what the latest reaction time was. Reset in the beginning of every trial.
    
        timer.reset()
        event.clearEvents() # get rid of other, unprocessed events
        while timer.getTime() > 0:
        for key, rt in event.getKeys(timeStamped=timer):  # time keys to this clock
            rts += [rt]  # add this reaction time to the list
            if key in ['escape']:
                core.quit() # quit if they press escape
            if key in ['b']:
                # add keypress to list for each keypress. then move cursor proportionally to length of this list
                prevPos+=stepSize
            if key in ['t']:
                prevPos-=stepSize
    
        # Log here instead of print
        print rts[-1]