Pause and resume trials

Hi,

I have this minimal working experiment below. The task: presenting a word in each trial and participants have to read the word aloud. What I am trying to achieve is that I want to be able to press ‘p’ key anytime during the trial to pause the experiment and press ‘any key’ to resume to go to the next trial in case participants do something wrong or fall asleep (yes, unfortunately).

I have tried to look into the forum and the one approach I like is from this one: Pause a loop with a key press

While the author seemed to be able to solve the problem, the code they provided there was not complete and I can’t make it work on mine.

In the trial routine, I have added in “each frame”:

Pause_check =0
if trial_key.status == STARTED:
    if (len(theseKeys) > 0 and 'p' in theseKeys):
        Pause_check =1

and in the cond_pause routine, I have added in “each frame”:

if Pause_check == 0:
    continueRoutine = False

While the experiment can run, pressing ‘p’ did not trigger to pause the experiment and give me this error code:
if (len(theseKeys) > 0 and ‘p’ in theseKeys):
TypeError: object of type ‘KeyPress’ has no len()

I would greatly appreciate anyone’s help on this.

Thank you so much!!

minimal_example.psyexp (130.3 KB)
stim_list.csv (49 Bytes)

The error TypeError: object of type ‘KeyPress’ has no len() indicates that at that point in time theseKeys is actually a single key not a list of keys, so you could just do if theseKeys=='p' at that point

Hi @jon, thank you so much for your help! This pause is now working.