Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Adding Conditional Changes in Sequence based on Keypress

I am working on creating a psych experiment. My goal is to create an experiment where a photo is shown for 2 seconds. After, participants are given the choice to press "1" to see the image again for 4 seconds or "9" to see a black screen for 4 seconds.

I have found that keyboard responses only collect keypresses and cannot change the sequence of the experiment. Is there a way to override this/change it with adjusting the code in the script? I have tried rewriting the sketchpad script (where the image is shown) to "show if: keypress=1" but this has been unsuccessful.

Looking for tips from someone with a more extensive background in coding!


  • Hi,

    yes, scripting will be necessary I think. But it is not terribly complicated. Check out the documentation to get an idea of how to use python code:

    It is not clear from your description how the sequence should end, but if seeing it once is enough, a regular sequence could be enough. If so, then you can simply have a sequence consisting of following items:

    • first stim
    • keyboard response
    • second stim (same visuals as first stim, but different duration)
    • alternative second stim (black screen)

    Only important thing to keep in mind here is that you have to use feedback time for the last two items, otherwise the experiment will crash (see [here]( The run if fields you can then set to the response that needs to be given ([response]=<value>)

    If it has to repeat multiple times, you would need inline script. Let me know if you need help, then I can try to implement an example for you.


  • Hi Eduard,

    Thank you for this advice! I already have the sequence you described but I'm not sure how to incorporate the run if fields. Do you know the "run if" code that would connect the keyboard response to the stimulus?

  • After a response is given, OS automatically creates a variables called var.response that represents the key that was pressed. You can use this variable to show/omit certain parts of the sequence, the way to do it is described in the beginner's tutorial for example, but the attached example also demonstrates it.

    Let me know if it still is not clear.


    Note: This is just a demonstration of the procedure, the code is not yet doing what you described above.

Sign In or Register to comment.