Search code examples
pythonpsychopy

Make text appear based on key press (give user real time feedback) in psychopy


I have a stimulus loop in psychopy that displays images for 4 seconds that subjects make binary decisions about. I would like to give them feedback as to which choice they have made.

I.e.: a image gets displayed for 4 seconds with white 'YES' and 'NO' displayed on either side of it. When the user presses a key, the corresponding word turns red. If they then press a different key, it switches. After 4 seconds, the next image appear with white words.

Does anyone know how to go about doing this? Many thanks for any suggestions.


Solution

  • You can do this with a custom code component. Add the code component to your routine.

    Under the "Each Frame" tab add the following code:

    if (t >=4) and (t < 8):
        if clear_keys:
            event.getKeys()
            clear_keys = False
        else:
            theseKeys = event.getKeys(keyList=['y', 'n'])
            if 'y' in theseKeys:
                Yes.color = 'red'
                No.color = 'white'
            elif 'n' in theseKeys:
                Yes.color = 'white'
                No.color = 'red'
    

    Under the "Begin Experiment" tab add the following code:

    clear_keys = True
    

    You will need to change the Yes and No objects in the script to the names of your text components. You will also need to change the number 4 to the start time of the picture and number 8 to the end time.

    Here is a picture of my trial as an example.