Perform part of script if experiment quits (Escape button)
in Expyriment
Hi,
I have some additional lines at the end of my script/experiment that are performed after
control.end()
***do something else***
Is there a way to ensure that this part of the code will be performed even if the Esc button is pressed during the experiment, and the program quits?
Thanks
Comments
Yes! As described here: https://docs.expyriment.org/expyriment.control.html#expyriment.control.end
You have two options:
Thanks for your quick reply.
Even though I've tried both ways they do not work. It only works if the experiment ends normally. If the Esc button is pressed meanwhile, this block of code is not performed. Below are the examples of what I've tried
Sorry I misunderstood before.
What you want to achieve is possible in two ways.
1. Within Expyriment, by manually defining how to process control keys (e.g. ESC) in all wait functions you use:
```
def custom_process_control_keys():
expyriment.io.Keyboard.process_control_keys(quit_confirmed_function=convert_variables)
# and then calling this explicitely as callback function in EVERY wait method in your code, e.g.:
exp.keyboard.wait(callback_function=custom_process_control_keys, process_control_events=False)
```
2. Independent of Expyriment, by registering an atexit function:
```
import atexit
atexit.register(convert_variables)
```
Hope that helps!
I am sorry, I don't know how to format code in here.
Here is the same answer correctly formatted:
https://gist.github.com/fladd/6e0c0af517b961c75c721b57fc66fab1
Great, that works! Thank you!