Howdy, Stranger!

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

Supported by

Presenting stimuli only while a button is pressed

edited September 2018 in OpenSesame

Hi to all,

I want to present a series of stimuli only while a certain button is pressed (i.e. the space button).
I already have the necessary part of the experiment for the presentation of the stimuli and the stimuli themselves.
Is there a way to do this?



  • Hi Amihai,

    There are multiple ways to do this, either with a script or coroutines item, but the easiest way depends on what you want to do exactly, and what you have programmed already. Could you upload the experiment here?


  • Hi Amihai,
    If you use an inline_script it may help to write a while loop, and ask for keylifts.
    You can find an example here:
    Good luck

  • Hi Sebastiaan, DahmSF,

    Thanks for the answers.

    Sebastiaan - I am attaching my experiment. Notice it is a bit "dirty" and has some scripts used for other purposes that I got from people in this forum (and for that I am grateful, I do not know how to code :smile: ). I want to add a script that will present the stimuli loop as long as the participant presses the space key. I would also appreciate advice on where to plant the script.

    DahmSF - I could not understand where is the example that you mentioned in the link you sent.

    Thanks again

  • Bump.
    Help anyone?

  • The example is in the experiment which is uploaded in the linked discussion. If you lift the button too early you get back to the beginning.
    --> There you need to keep the mouse button pressed, but you can easily change it to the space key on the keyboard.

  • DahmSF,

    I do not really know how to code so I cannot identify the specific part of the code that enables to present stimuli while a button is pressed. Can you point me to this part?
    Also, in my experiment, I measure the duration of the pressed button so it is a dependent variable. I do not want the experiment to go back to the beginning at any point. is the code in your example appropriate for this?


  • Hi Amihai,

    did you try the experiment i attached in the link?
    It contains not much more than pressing a button and keeping it pressed while some stimuli are presented.
    The duration you are asking for is easy to code. Just create a new variable: time of keylift - time of keypress.
    You can also look into the handbook for the keylift function Keyboard.get_key_release:

    Even if you do not want the experiment to go back to some point you need to specify what shall happen if a participant lifts the key too early. (I thought the best thing is to go back and start the trial again)

    You re welcome

  • Hi Stephan,

    I tried to take a look at the experiment but it did not work for me for some reason.
    I did look at the code in it though and saw that it is way above my level of understading.
    At this point, I think I will seek paid assistance.
    Do you know if there someone in this forum that offers such services?


Sign In or Register to comment.