Psychopy won't detect keypresses

Hi I have a n-back task which detects spacebar keypresses using the following code.

# Setting up trial components
nback_clock = core.Clock()
nback_clock2 = core.Clock()

nback_kb1 = keyboard.Keyboard()
nback_kb2 = keyboard.Keyboard()
fix_cros = TextStim(win, text='+', height=0.15)

# Loading stimuli
nback_letters1 = pd.read_csv('nback_letters1.csv')['letters'].tolist()
nback_corrAns1 = pd.read_csv('nback_letters1.csv')['corrAns'].tolist()

nback_list1 = list(chunks(nback_letters1, 48)) 
nback_corrAns = list(chunks(nback_corrAns1, 48)) 

# Data saving
nback_results = pd.DataFrame(columns = ['Trial number', 
                                        'Stimulus presented', 
                                        'Key pressed', 
                                        'Correct answer', 
                                        'Reaction time'])

# Begin trials for n-back 1 
for i in nback_list1:
    for j in range(0, len(i)):
        nback1_start =
        if kb.getKeys(keyList=['escape']):
        corrAns = nback_corrAns1[j]
        thisLetter = nback_letters1[j]
        letter_present = TextStim(win, text=nback_letters1[j], height=0.5)
        if j != 0:
            # Use clock-based timing
            start_marker_sent = False
            tStart =nback_clock.getTime()
            while nback_clock.getTime() < 2.5:
                if nback_clock.getTime() < 0.5:
                    if start_marker_sent == False:
                        win.callOnFlip(port.write, 'F'.encode())
                        start_marker_sent = True
                keys_n = nback_kb1.getKeys(keyList = ['space']) 
                tResp = nback_clock.getTime()
                response = 'None' if not keys_n else keys_n[-1]
            # Get responses 
            if keys_n:
                RT = (tResp - tStart)-0.5
                if corrAns == response or str(corrAns) == response:
                    answer = 1
                elif corrAns != response:
                    answer = 0
            elif response == 'None':
                RT = 0
                if corrAns == '' or corrAns == [] or corrAns == None or corrAns == 'None':
                    answer = 1
                    answer = 0
                answer = 'error'
            # Saving trial data
            thisExp.addData('N back RT',RT)
            thisExp.addData('Stimulus presented',thisLetter)
            thisExp.addData('N back response',answer)
            thisExp.addData('N back start',nback1_start)
            thisExp.addData('Key pressed', response)
            thisExp.addData('Trial number', j)
            # Saving response data
            nback_results = nback_results.append({'Trial number': j, 
                                                    'Stimulus presented': thisLetter,
                                                    'Key pressed': response,
                                                    'Response': answer,
                                                    'Reaction time': RT}, ignore_index=True)
            while nback_clock.getTime() < 2.5:
                if nback_clock.getTime() < 0.5:

When I then look at the csvs it says response = None and that a key wasn’t pressed. Why is this happening? I would appreciate any help :slight_smile: Thank you!

I think the issue is that the call to:

keys_n = nback_kb1.getKeys(keyList = ['space'])

is within a 2.5 second loop, so it will be called repeatedly during this time period, even if a key event has already been detected, overwriting the earlier detected key. To fix this, only get keys_n until one is detected during the 2.5s seconds, then stop checking for keys and recalculating response.

The other thing is that:

keys_n = nback_kb1.getKeys(keyList = ['space'])

will return the key after a release is detected, not when the key is just pressed. If this is intended, leave the code as is, otherwise use waitRelease=False:

# return a key when it is pressed, not when it is released
keys_n = nback_kb1.getKeys(keyList = ['space'], waitRelease=False)
1 Like

Perfect, thank you! How does one get it to stop checking keys after a response?

Would one use a boolean key_pressed = False before the while loop begins & then a key_pressed = True after the getKeys function? Although this would still overwite itself every loop…