Use a text response and submit only when something is typed

Hi there,

Yes there is! But it involves some code components. You essentially need to check if the textbox currently contains anything and then end the current routine only if it does contain something.

In this demo:

textbox-complete.psyexp (11.5 KB)

I first make a function (in the before experiment tab so that it works online) to check if the textbox is what I would class as empty (has only a space or nothing):

def checkEmpty(text):
    if text == " " or text == "":
        return True
    else:
        return False

Then in the each frame tab I use a code component to watch for key presses and submit:

# watch for key presses
keys = event.getKeys()
if 'return' in keys:
    # do not show a new line break in the typed response
    textbox.text = textbox.text[:-1]
    # check if the response is empty and only allow the routine to end if something has been typed
    continueRoutine = checkEmpty(textbox.text)
    # clear the keyboard keys and watch for a new response
    event.clearEvents('keyboard')

Hope this helps,

Becca

1 Like