Typed response, "backspace" and "space" only work once

Windows 10, Version 2020.2.4, Standalone

Hi, I’m building an experiment where participants read a series of sentences then recall the last words of each sentence. The recalled words are separated by space, and participants should be able to modify their answer if needed. Here’s my code

if("backspace" in key_resp_5.keys):
    if(len(key_resp_5.keys) > 0):

elif("space" in key_resp_5.keys):
    key_resp_5.keys.append(' ')
elif("return" in key_resp_5.keys):
    key_resp_5.keys.remove("return") #remove return
    if(len(key_resp_5.keys) > 0):
        screen_text = ''.join(key_resp_5.keys)
        thisExp.addData("simple_recall", screen_text) #store response in data file after entering

    continueRoutine = False

The problem I have is that Backspace and Space only work once. For example, if somebody typed “red”, one press of backspace works fine “re”, another press the on screen text becomes “red” again, one further press makes it “redbackspace”. Same patterns goes with “space” key as well. I’m wondering how to fix this. I did refer to TextInput experiment, but still unable to figure this out.

You don’t need to code this yourself anymore. Delete your text stimulus and replace it with the new TextBox component- this can handle editable text for you, with no need for custom code.

I do realize there’s a new TextBox component, but I want to evaluate people’s input with correct answer regardless of the order. Say, the word order as they were shown can be [‘bird’, ‘tree’, ‘red’], participants enter [‘r’, ‘e’, ‘d’, ‘t’, ‘r’ ,‘e’, ‘e’, ‘b’, ‘i’, ‘r’, ‘d’] should still get full mark, and the program should be able to show the number of words recalled correctly even participant only recalled 2 out of 5 words. Is the Textbox compatible with what I’m trying to achieve?

I can kind of see how it would work, rather than comparing the ''.join(key_resp_5.keys) with the correct answer, I now need to compare ''.join(textbox.text), is this how it works?

I tried TextBox plus some original code I wrote to evaluate input, now it’s working perfect!