PsychoPy getkeys inconsistent behavior

Hi all,

I programmed a relatively simple experiment using the PsychoPy Coder (Python 3.7.6 under Win10), in which participant are asked to press a button (left & right arrow key) within a fixed time window. Approx. 90 % of the trials everything works well and I get what I need (i.e. key name, reaction time), but in 10 % of the trials getkeys misses the key press. I wasn’t able to find detect any other unusual behavior (e.g. irregular timing). I would be very greatful for any hints, since I am quite new to programming with Python.
Many thanks in advance!!

Janine

This is the relevant code snippet (df is a panda data frame):

kb = keyboard.Keyboard()

continuing = True

logDat.write("START TRAINING PHASE; Time:" + str(exptime.getTime()) + "\n")
for i in range(len(df)):

    # some if conditions removed for clarity

    psychopy.event.clearEvents()

    name = df.loc[i,"stimname"] + ".png" # choose pic 2 show
    im = os.path.join(stim_path, str(name))
    Stim = visual.ImageStim(win,pos=(0,0), image=im)

    grating.draw()
    fixcross.draw()   
    win.flip()  # show fix cross

    logDat.write("FIXATION CROSS\n\n")
    grating.draw()
    message.draw()   
    Stim.draw()
    yes.draw()
    no.draw()
    win.flip()  # show stim along with response options
    logDat.write("SHOW STIM " + name +"; Time:" + str(exptime.getTime()) + "\n")
    df.loc[i,'stim_time'] = exptime.getTime()
    
    timer = core.CountdownTimer(resp_dur) # set timer to response time
    kb.clearEvents(eventType='keyboard')
    kb.clock.reset() # reset keyboard clock
    while timer.getTime()>0 and continuing:
        Key = kb.getKeys(keyList=['left', 'right', 'escape'], waitRelease=False)
        df.loc[i,'resp_time'] = exptime.getTime()
        if 'left' in Key :   # "yes" response
            logDat.write("Left Key pressed; RT:" + str(Key[0].rt) +"\n")
            df.loc[i,'rt'] = Key[0].rt
            df.loc[i, 'resp'] = 1 
            if (int(correct_ans == 1)):           
                im = os.path.join(stim_path, 'instr\\correct.png')
                Stim = visual.ImageStim(win,pos=(0,0), image=im)
                df.loc[i,'cor'] = 1
            elif (int(correct_ans == 0)):
                im = os.path.join(stim_path, 'instr\\false.png')
                Stim = visual.ImageStim(win,pos=(0,0), image=im)
                df.loc[i, 'cor'] = 0
        elif 'right' in Key:       # "no" response
            logDat.write("Right Key pressed; RT:" + str(Key[0].rt) +"\n")
            df.loc[i,'rt'] = Key[0].rt
            df.loc[i, 'resp'] = 2
            if (int(correct_ans == 1)):           
                im = os.path.join(stim_path, 'instr\\false.png')
                Stim = visual.ImageStim(win,pos=(0,0), image=im)
                df.loc[i, 'resp'] = "2"
                df.loc[i, 'cor'] = "0"
            elif (int(correct_ans == 0)):
                im = os.path.join(stim_path, 'instr\\correct.png')
                Stim = visual.ImageStim(win,pos=(0,0), image=im)
                df.loc[i,'resp'] = "2"
                df.loc[i,'cor'] = "1"
        if 'escape' in Key :   # exit
            df.to_csv(outname, index = False, sep = ";")
            core.quit()
               
    if df['resp'][i] == -1:                        # if none response was given on time
        im = os.path.join(stim_path, 'instr\\too_late.png')
        Stim = visual.ImageStim(win,pos=(0,0), image=im)
        df.loc[i,'cor'] = "0"
    
    if df.loc[i,"phase"] == 1: # show feedback only during first phase
        grating.draw()
        message.draw()   
        Stim.draw()    
        win.flip()  # show feedback/ "too late" notification
        core.wait(fb_dur)
    
    logDat.write( str(df.loc[i,['cond', 'stimid', 'resp', 'rt', 'cor', ]]) + "\n")

It could be that the keys are being picked up and not used - what happens if you change:

Key = kb.getKeys(keyList=['left', 'right', 'escape'], waitRelease=False)

to

Key = kb.getKeys(keyList=['left', 'right', 'escape'], waitRelease=False, clear=False)

?

1 Like

It worked! Thanks a lot :-)!

1 Like