End loop after time limit in task using text-input

OS: Mac OSX
PsychoPy version: v3.0.0b11
Standard Standalone? (y/n) Y

Hi – I’m new to PsychoPy and I could use some guidance!

I am trying to put together a task where participants see a cue word on screen for 60 seconds. While the word is onscreen, participants will type in words as many related words as they can think of, in a fixed amount of time. Ideally, after typing out each word, they can press enter, and the word would disappear off screen, leaving a blank screen so they can continue to type in new words. As I mentioned before, they would have 60 seconds to do this, after which point the next cue word appears. Participants would then have 60s to come up with words related to the new cue word, and so on.

I used the code here to create an experiment where participants see my list of cue words and are able to freely type text in response to each word. However, I cannot figure out how to write the code to specify that pressing enter clears the screen and saves the response while still remaining on a given cue word until the 60s elapse. Currently, as I have it set up, participants can only type in one response to a given cue word, and because once they press enter, the loop ends and the next cue word appears.

Any help on this front would be really helpful! And if I am lacking any needed specificity, please let me know!

Here’s the code for the compiled experiment, as I have it so far:
FreeAssociation_Task_Scratch.py (19.4 KB)

Can you please post the exact code you are actually using here? It will make it easier to suggest the necessary change (which should be simple).

Hi @Michael,

I’ve edited the original post to have the compiled experiment as I have it so far. Thanks for your time/help!

I’ve also attached a screenshot of the builder interface, in case that helps:

B

Please just copy and paste the code from within your code component (it is quite laborious to wade through an entire generated script with all of its boilerplate code: we just need to see your custom code that handles the keypresses and updates the text stimulus).

The screenshot above is useful to give us an idea of your overall experiment and the names of your components. But please also provide a screenshot of the settings from your keyboard component key_resp_4.

Got it!

Here is the code:

Begin Experiment:

inputText = ""

Begin Routine:

theseKeys = ""
shift_flag = False
text.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
 
    if 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:

thisExp.addData('inputText', inputText)
inputText = ""

Also, a screenshot of the settings for key_resp_4:

OK, have updated your code to work with two variables: one (wholeText) keeps track of everything that has been typed so far in this trial, while one (currentText) just keeps track of the current item. It is this variable that should appear in your text component text field as $currentText, set to update every frame.

Begin Experiment: Don’t actually need anything here

Begin Routine:

#theseKeys = '' # probably unnecessary as I guess Builder controls this variable
wholeText = '' # everything typed in the trial
currentText = '' # just the current line/word
shift_flag = False
text.alignHoriz = 'left' # probably unnecessary to set in code, as it isn't changing?

Each Frame:

n = len(theseKeys)
i = 0
 
while i < n:
    if theseKeys[i] == 'return':
        # pressing RETURN adds the current word to the stored responses:
        wholeText = wholeText + currentText + '\n'
        currentText = '' # and clear what is currently displayed on screen
 
    if theseKeys[i] == 'backspace':
        if len(currentText) > 0:
            currentText = currentText[:-1]  # lose the final character
 
    elif theseKeys[i] == 'space':
        currentText += ' '
 
    elif theseKeys[i] in ['lshift', 'rshift']:
        shift_flag = True
 
    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:
                 currentText += chr( ord(theseKeys[i]) - ord(' '))
                 shift_flag = False
             else:
                 currentText += theseKeys[i]
 
    i = i + 1

End Routine:

thisExp.addData('inputText', wholeText)
1 Like

Thanks, @Michael. That works perfectly!