| Reference | Downloads | Github

Inverted response time task


I am very used to using the coder but since pavlovia only uses the builder I am transitioning to using it to be able to run online experiments. But I am stuck on how to achieve a simple generation task, where instead of the stimuli first appearing and then the participants responding, I want the participant to press a key and then the stimuli will appear on the position pressed by the participant. This would be on a loop. Can one achieve this in the builder and then java without resorting to if statements?


Can you describe that more precisely? i.e. keys don’t have positions, so how do you define the correspondence?

Sorry, I am so used to thinking about the task that I missed an important point. I would have 4 squares arranged on the screen and each square would have a corresponding key. Depending on the key pressed by the participant, an image would appear in that square.

OK, that is clear. You need a few snippets of custom code, so insert a Code Component (from the “custom” component panel) into the routine where the stimuli will appear (there should be a separate routine to collect the keypress, which ends when the keypress occurs). First you need to set up the correspondence between the keys and their associated positions. This just needs to happen once, so put this code in the “Begin experiment” tab:

# create a dictionary that links keys to positions.
# replace with your actual values:
positions = {'a':[-200, 100], 'b':[200, 100], 'c':[-200, -100], 'd':[200, -100]}

Then in the “begin routine” tab, put something like this:

pressed_key = your_keyboard_component_name.keys[0]
# look up and apply the associated position:
your_stimulus_name.pos = positions[pressed_key] 

Make sure the code component is above the stimulus component, so the latter gets its position updated before it is drawn.

But bear in mind that you can directly click on stimuli and select them that way, rather than using keys. If that suits what you need, just put the names of your four selectable stimuli in the “Clickable stimuli” field of a Mouse component.

The position of the clicked stimulus is then available to you under the name:


I actually need to use the keyboard, but thank you for your suggestion! What should I put on the stimuli position property?

It doesn’t matter, some arbitrary value that is set to be “constant”. That way it will be overwritten by the suggested code above.

Do you know any way that would allow the image to be on screen until the next key is pressed? Right now if I don’t add a timer to the image, it just stays in the same position forever.

You just put another keyboard component on the routine where you show the stimulus, set to have an indefinite duration and to “force end of routine”. The stimulus also needs to have an undefined duration.

Thank you for answering, I thought of doing that, but that means that it will always require two key presses for the stimuli to change, one to signal end of routine and another to indicate the position.
What I am aiming to do is to have one key press that indicates the position of the image that remains on screen until another press that makes the position change until another press and so on.

I’m confused. Your question was “Do you know any way that would allow the image to be on screen until the next key is pressed?”.

I think you need to explain in detail your intended task procedure. We must be talking at cross purposes.

Sorry if I was not explicit enough, I basically want to achieve the following. Participants will observe 4 rectangles and asked to generate a sequence of 100 key presses. Each key will give them visual feedback of which key they pressed. So if they pressed the key Z the image will appear in the rectangle on the left. Each key press should give them feedback until another key is pressed. So if I press Z the image will appear on the left, then I press X and the image will change to the centre left, then I press N and it will change again… and so on for 100 trials. What I have been finding difficult is to ensure that the image appears in a new position as soon as another key is pressed. If I leave the image with no time restrictions it just stays there forever.

OK, the overall scheme is starting to make more sense but you still need to explain the original arrangement of the stimuli (in detail) and the correspondence between what key is pressed and what a given image does. The issue here is that you know exactly what you want to achieve, but I can’t tell you how to implement it unless it is broken down precisely. Think “What will a person understand of my task, knowing nothing other than what I wrote down here”?


  • describe the layout of the stimuli
  • name all the keys
  • state the action associated with each key

At the moment this is all too implicit, and computer code needs to be the exact opposite of that.

The good news, though, is that I think what you want can be achieved relatively simply

I am sorry if I was not clear enough.

Basically this is an inverted reaction time task, instead of an image appearing and you pressing the key that corresponds to that position, you do the inverse, where you press a key and an image appears in the corresponding position.

So I ask participants to generate a sequence of 100 trials, they are given only this directions:
Z corresponds to position left;
X corresponds to position centre left
N corresponds to position centre right
M corresponds to position right.

Then they are given 100 trials to try to guess the sequence that they learned in another task. So they will press the keys in the order that they thought appeared before and after pressing each key, the image will appear in the corresponding position. So they might try the sequence XMN and the image will appear after each pressing in the corresponding position so they would see centre left, right, centre right. The change in position of the image is just to give them feedback of the keys that they pressed and it should remain on the screen until they press another key.

Ah, OK, so there is just one image? If so, then this is pretty straightforward.

  • Put a loop around your routine that is set to have an nReps of 101, and is not connected to a conditions file.
  • Create your image stimulus component, and give it an initial position that would put it off screen (i.e. so that it is initially invisible) and is set to be “constant”. This way we can over-ride the position in code, as Builder will set its location only once, at the start of the experiment. Give it an unspecified duration.
  • Insert a keyboard component, again with an unspecified duration, and with allowed keys of 'z', 'x', 'n', 'm', and set to “force end of routine” (so that the feedback will be immediate).
  • Insert a code component (from the “custom” component panel). In its “End routine” tab, put something like this:
pressed_key = your_keyboard_component_name.keys[0]

if pressed_key == 'z':
    x = -0.5
elif pressed_key == 'x':
    x = -0.25
elif pressed_key == 'n':
    x = 0.25
elif pressed_key == 'm':
    x = 0.5

your_image_stimulus_name.pos = [x, 0]

I’ve used values in norm or height units above for the x position. Change those to what you want, or to equivalent pixel values or whatever units you are using, as required.

Hopefully the above makes sense: the routine will repeat 100 times, collecting a keypress response, and updating the position of the stimulus for the next time the routine runs (which will be immediately). This is why nReps has been set at 101, because the routine runs the first time with no stimulus shown. i.e. the feedback is shown on the immediately following iteration of the loop, rather than on the one in which the key is pressed, but to the participant, the whole thing will seem like a continuous response/feedback task.

Does this make sense/do what you want?

Thank you! That worked perfectly! Sorry for taking so much out of your time! I wish I could somehow repay it.