[solved] Breaking a loop by key press? How do I do that?
Hello everyone,
I'm doing a project for a class and I'm a strait up newbie at this program or Python coding (no idea how it works).
I created an experiment that is based on time response but I can't seem to figure out how to make a loop stop by a key press (example: spacebar) which is crucial for the experiment and for the data. I tried reading the discussions about similar problems but I have really no idea how Python works so I just got lost by trying different codes which didn't work. Can someone please help this newbie? Thank you in advance ![]()
Comments
Hi,
First of all, welcome to the Forum! The easiest way to learn about Opensesame (and some python), if you do one of the tutorials (e.g. this one).
What you can do is defining a variable that is
Trueif you want to run aloopandFalseif not. Then you only have to monitor your keypresses and set the variable toFalseas soon asspaceis pressed.As for the code you'll need an
inline_scriptbefore the loop you're talking about, in which you can initialize your breaking variable:breaking_variable = Trueand another
inline_scriptin the end of your loop sequence (preferably in itsrun_phase) in which you can set the variable to False, if the response is 'space'.Finally, you have to set the
run iffields to[breaking_variable] = True, for every element in theloop.If you get stuck, ask again with your specific problem or have a look on the documentation of Opensesame. Most of the information you need, you can find somewhere there.
Good luck,
Eduard
Hello Eduard, Thank you so much for you fast response. I did the tutorials before and that's the way I figured out how to use the program it's just I got stuck at this point because I have to use Python coding which just confused me haha.
I understand defining the variable to change stimuli or get the right or wrong response but I don't know how I can define a variable the way you said: so "it is True if I want to run a loop and False if not." Everything else I did as you said. Can you help me with that?
Basically I'm recreating a visual experiment where a participant needs to see differences between two stimuli where one have been altered and press space bar when they see the change and to report verbally where the change was. I created a loop with 12 variables that has another loop altering one variable with the original image and the modified version and basically I want the variables changing when they recognized the change (pressed space bar). I made the stimuli to alter but I cant make it stop by pressing a key.
Here's basically what I did.

Thanks again for your help,
Kat
Hi Kat,
So you want this loop, in which you present modified and unmodified images alternatively to break, as soon as participants indicated per keypress that they saw the change?
If so, I recommend you simplify your
Imageloop_sequencea bit. Instead of showing every image in a sketchpad you can move stimulus presentation to aninline_script, because otherwise you would have to include as manyinline_scripts as you havesketchpads. The code below, put in aninline_scriptshould be enough to replace everything inside yoursequence. I didn't test it very thoroughly though. Probably you have to adjust some things. Also, please make sure that everything what you need is logged!Good luck,
Eduard
thank you a bunch for your help!!
Hi eduard,
I tried what you described here. So, in order to break a loop, I put
breaking_variable = Truein an inline-scripte before the loop, and an inline-script containingif exp.response == 'space': breaking_variable = Falsein the last position of the loop (in the loop is just one sketchpad). Then I put
[breaking_variable] = Truein the run-if block of the sequence.However, my experiment does not run at all, the error message says "The variable 'breaking_variable' does not exist."
What did I do wrong?
Thank you!
Hi Pia,
Your experiment does not run because
[breaking_variable]refers tovar.breaking_variable, notbreaking_variable. See what I mean?Btw. Not that it matters, but it is a little unintuitive that your loop runs if breaking variable is True and breaks when it it False.
Eduard
Hi eduard,
thank you. I think I understand, but when I change braking_variable (in the inline-scripts only) to var.breaking_variable I get the following error:
AttributeError: response not foundSo I changed the duration of the sketchpad to 0 (instead of keypress) and included a keyboard_response-item.
But I still get this error-message!
Hi Pia,
The script I wrote 2014 didn't really work.
This makes more sense:
Btw. Are you aware that the script takes care of both stimulus presentation as well as response collection? So if you want to combine it with a sketchpad you have to be careful to not mix things up and get into trouble.
Does that make sense? Feel free to upload your experiment, if you can't get any further.
Eduard
Hi eduard,
thank you! However, I have only one sketchpad therefore I didn't use that script in the first place. I only included the inline-scripts to define the variable and to break the loop.
So, I have a sketchpad, which is in a loop and the position of a rectangle of the sketchpad changes with each iteration of the loop. Participants should be able to break the loop if they feel it is at the right position.
I'll upload my experiment. It is the first loop, I'm working on.
Hi Pia,
I felt free to implement your design in an
inline_script. I tried first to make it work withsketchpads andloops but it wasn't really working. Sorry for that. The good thing though, is that this implementation is really easy and rather intuitive.Let me know if anything is unclear.
Eduard