3

Seems a silly question but I can't find in the Docs how to get text input from the user in a PsychoPy Builder experiment.

e.g if I had a Text component to display the message "What is your name?"

I want to get the text answer (with key echoing to the screen) and save it to the results file.

Will I have to drop into code and use a dialogue box?

Thanks

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28
jacanterbury
  • 1,435
  • 1
  • 26
  • 36

2 Answers2

4

Thanks Michael. Looked like it would do the trick but I was hoping for something simpler to hand over to a non-programming researcher so I had a play and came up with the following which satisfies my requirements.

I've stuck the code into GitHub here https://github.com/jacanterbury/PsychoPy-snippets

but basically it does the following:

TextStim text field contains this:

$(word + '\n' + inputText)

A code object in the same loop has this:

Begin Experiment:

inputText = ""

Begin Routine:

theseKeys="" 
shift_flag = False
text_3.alignHoriz ='left'

Each Frame:

n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return':
        # pressing RETURN means time to stop
        continueRoutine = False
        break

    elif theseKeys[i] == 'backspace':
        inputText = inputText[:-1]  # lose the final character
        i = i + 1

    elif theseKeys[i] == 'space':
        inputText += ' '
        i = i + 1

    elif theseKeys[i] in ['lshift', 'rshift']:
        shift_flag = True
        i = i + 1

    else:
        if len(theseKeys[i]) == 1:
            # we only have 1 char so should be a normal key, 
            # otherwise it might be 'ctrl' or similar so ignore it
            if shift_flag:
                inputText += chr( ord(theseKeys[i]) - ord(' '))
                shift_flag = False
            else:
                inputText += theseKeys[i]

        i = i + 1

End Routine:

# let's store the final text string into the results finle...
thisExp.addData('inputText', inputText)
inputText=""

The results file in the data folder gives the individual keys pressed as well as the final string

Hopefully the code is fairly self-explanatory. The only thing that might not be obvious is that the lower & upper case characters in ascii are 1:1 & 32 apart which is the value of a single white space (e.g. in ASCII 'a' = 'A' + ' ' )

Feel free to comment/improve on it (it doesn't handle SHIFT-LOCK at present but that should be easy to fix)

Would be nice to drop it into a library so that all the code on the 'Each Frame' tab could be swapped for a single line

jacanterbury
  • 1,435
  • 1
  • 26
  • 36
2

There are two alternatives: (1) use a dialog box as you mention. You either need to have the main window not be full-screen, or temporarily minimise it so that the dialog can be seen in front.

(2) Handle keypresses yourself and mirror them to the screen in a text component. Yes, it would be nice if this was built-in, but it isn't currently. Read through this extensive thread for some suggestions of how to incorporate this approach into Builder: https://groups.google.com/forum/#!topic/psychopy-users/lE_bTMHUAoU

Also see this thread: https://groups.google.com/forum/#!topic/psychopy-users/DGXkU-31MPg with a pointer to some code form Alex Holcombe.

Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28