Conditional response based on keyboard press and screen position

Hello everyone. I am using Windows/PsychoPy v.2020.2.4 and I am trying to build an experiment using the Builder Mode. The basic idea for now is that there are three images (image1, image2, image3) presented on screen on random position each time (pos1, pos2, pos3). During the images presentation there is keyboard response [‘left’, ‘down’, ‘right’] for basically choosing the image on the left, on the middle or on the right.

Based on the button response and the position of the image I am creating a new variable x which I also want to save it later on in the data output.

I normally presented my images on random position. Possible positions are:

pos1 = [0, 0]
pos2 = [0.25, 0]
pos3 = [(- 0.25), 0]

Ultimately the goal will be to have a custom code like this:

if (responsekey.keys == 'left' and Image1position == pos3) or (responsekey.keys == 'right' and Image1position == pos2) or (responsekey.keys == 'down' and Image1position == pos1):
    x = "Image1"
elif (responsekey.keys == 'left' and Image2position == pos3) or (responsekey.keys == 'right' and Image2position == pos2) or (responsekey.keys == 'down' and Image2position == pos1):
    x = "Image2"
elif (responsekey.keys == 'left' and Image3position == pos3) or (responsekey.keys == 'right' and Image3position == pos2) or (responsekey.keys == 'down' and Image3position == pos1):
    x = "Image3"
elif responsekey.keys == None:
   x = "NoSelection"

This doesn’t work at all, so I’ve decided to go simple and just have a keyboard response and at least return if they pressed left, down or right key.

And then I am adding a custom code right after the keyboard response, in the Begin Routine tab:

if responsekey.keys == 'left':
    x = "left"
if responsekey.keys == 'right':
    x = "right"
if responsekey.keys == 'down':
    x = "down"
    x = "late"

And then I am having a text stimulus screen, where text is " $x ".
But then it only returns the “late” as text, it never goes through the if statements. If I remove the else statement then it doesn’t run.

If I ultimately manage to run this, how can I modify the output from the builder mode and save this new variable created inside the custom code?


This is solved by adding the next stimulus on a new routine. Then the new variable is saved in memory


Pleased you found your solution!
As a pointer to reduce your code component there, you could use:

if responsekey.keys:
    x = response.keys
   x = 'late'