| Reference | Downloads | Github

How to Reset Keypresses

Hi Everyone,
I am having some trouble resetting the keypresses after each trial.

Here is my code:

def showstim(stimtype): # get a key record function in here
    selstim = eval(stimtype).pop(0)
    drawstim = visual.ImageStim(win, image = selstim, pos = [0,0])
    #trialnum = trialnum + 1
    keypress = []
    while frames < my_dur:
        stimonset = exp_clock.getTime()
        frames = frames + 1
    keypress = kb.getKeys(['right', 'left', 'q'], waitRelease=True, clear= True)
    if frames >= my_dur:
        stimoffset = exp_clock.getTime()

def recresp(keypress):
    for key in keypress:
        taskd['encrespkey'], taskd['encrt'], taskd['enckeydur'] =, key.rt, key.duration
        if ('left' in keypress) and (stimtype == 'stanpics'): # left is abstract
            taskd['enckeyaccuracy'] = 1
        if ('right' in keypress) and (stimtype == 'stanpics'):
            taskd['enckeyaccuracy'] = 0
        if ('left' in keypress) and (stimtype != 'stanpics'):
            taskd['enckeyaccuracy'] = 0
        if ('right' in keypress) and (stimtype != 'stanpics'):
            taskd['enckeyaccuracy'] = 1

I set the ‘clear=True’ within the kb.getKeys component. However, every time there are no responses, this code just prints out the key presses from the previous trial.

I was wondering if anyone knows how to ‘wipe’ the kb.keypress buffer from old (i.e., previous trial) clicks.

Much appreciated,

kb.getKeys(....., clear= True) tells the keyboard to remove / clear the returned events from the keyboard buffer; any keyboard events not returned by the call will stay in the event buffer.

To clear the full keyboard event buffer, use:

1 Like