psychopy.org | Reference | Downloads | Github

Use a text response and submit only when something is typed

Hello,

I’m a beginner on psychopy and i’m following several tutorial. But I have a question today. From the builder, how can I include a text response, that is the stimuli appear (that’s ok) and participants need to write some text and click on “space” or “enter” to go to next trial ?

Thanks for your help

KB

Hi there,

You can use the “textBox” component to record typed responses in the most recent release of PsychoPy :slight_smile:

Please note that since this is a recent addition, it is in beta phase.

Thanks,
Becca

Hello,

Oh great, it is perfect ! Thank you for your answer !
I have a last question. I use the textbox, but how do for that participants can press on “space” or “enter” or an other keyboard letter to validate the answer and go to the next stimulus ?

Thanks

KB

Hi,

You pick up on a good question, this is currently something being ironed out in the textBox component. For me so far I have added a keyboard component to the routine that force ends the routine, but that means that in the keylist of this component you need to make sure to list keys that the participant would not use whilst typing (this is tricky because they could even use the arrow keys to move the cursor in the text box). A better solution suggested by one of the team today was to use a mouse component to end the routine, and allow “any click” to force end the routine.

The trade off is that participants might be slower to register a response after typing with a mouse click compared with a keyboard keypress - so it depends if response time is important for your study.

Hope this helps,
Becca

Hi,

Thanks for your solution, that’s work! It’s perfect.

Thank you for your help
Have a nice day

Best Regards
KB

No problem, if you could mark the solution for future users that would be great.

Thanks,
Becca

I use the mouse component to end the routine, because response time is not important in my study. Thus, participants write their answers, I have added a text to component (“click to go to the next trial”), and any click forces the end the routine.

KB

Hi Becca,

Is there any way to make sure participants made a typed response before ending the routine? I’ve an experiment with a task asking for a typed response, but had noticed that participants can skip answering many questions, i.e. not typing any thing, but simply ending the routine using a keyboard component ‘Enter’.

Thank you

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

Hi Becca,

Thank you for your response. I understand that thses codes are used with the text.box function. Unfornately I didn’t use this function but I the following codes that work well with me except for enforcing response.

so in begin experiment tab I have:

inputText3 = ""

In begin experiment tab:

inputText3 = ""
actualKeys=""
shift_flag = False
event.clearEvents(‘keyboard’)

In each frame tab I have:

actualKeys = event.getKeys()
n = len(actualKeys)
i = 0
while i < n:
if actualKeys[i] == ‘return’:
continueRoutine = False
break
elif actualKeys[i] == ‘backspace’:
inputText3 = inputText3[:-1] # lose the final character
i = i + 1
elif actualKeys[i] == ‘space’:
inputText3 += ’ ’
i = i + 1
elif actualKeys[i] in [‘minus’, ‘-’, ‘num_subtract’, ‘Minus’, ‘slash’]:
inputText3 += ‘-’
i = i + 1
elif actualKeys[i] in [‘lshift’, ‘rshift’, ‘SHIFT’]:
shift_flag = True
i = i + 1
else:
if len(actualKeys[i]) == 1:
if shift_flag:
inputText3 += actualKeys[i].upper()
shift_flag = False
else:
inputText3 += actualKeys[i]
i = i + 1
In end routine tab:

thisExp.addData(“typedWord3”, inputText3)
inputText = ""

I’m not quite sure what changes should I make to these codes to end up with the same results, i.e. having at least 3 input characters to end the routine.

Best,
Ghozayel

if actualKeys[i] == ‘return’ and inputText3 != ‘’:

Thank you so much for this, Wakecarter.

I’ve tried this and it works, participants now cannot move to the next question unless they input at least 1 character. Do you have any idea how to make it to ‘at least 3 charachters’ to be able to proceed?

if actualKeys[i] == 'return' and len(inputText3) > 2: