As a general advice, it will be easier for you in the long run to add it in a builder code component. Going from builder to coder is a one way street - edits in code cannot be brought back into builder. Or you can go full code in which case it is adviced to write it from scratch.
Your implementation
A few comments on reasons why your current code doesn't work:
- Every event.getKeys() will per default delete the responses after it has been called. Thus calling it multiple times in each frame like you do will make the subsequent calls empty.
- event.getKeys() takes a LIST as argument where you gave it a string. If you want to listen for a single key, give it ['return'] rather than 'return'.
And as an advice to improve timing (and conceptual clarity): if the visual is static while waiting for keypresses, I would just use waitKeys so something only happens on keypresses. That would also increase the accuracy relative to getKeys() which is locked to frames when put in a loop with win.flip().
Solution
So you could do the following in a code component, which should be below the text:
while True: # Yes, continue listening forever...
resp_key = event.waitKeys(keyList=['left','right','up','down','return'])[0] # pick first and only response.
# Set different texts dependent on received response
if resp_key == 'left':
yourTextStimulus.text = '5'
elif resp_key == 'right':
yourTextStimulus.text = '7'
elif resp_key == 'up':
yourTextStimulus.text = '2'
# .. or break out of the loop if return was pressed
elif resp_key == 'return':
break # oh, do NOT continue forever... we're finished on return :-)
# Actually show text if loop was not ended
yourTextStimulus.draw()
win.flip()
Rename "yourTextStimulus" to the name of your text. Of course you can tweak this to more/different keys and make the actual strings (currently '5','7','2') trial-dependent if you need to.