Saving keypresses from each iteration of loop to data file

Hi all,

I am relatively new to Psychopy/Pavlovia and to code, so apologies if this is a little basic.

I am looking to use a code component in my experiment to save all keypresses from a routine which has a loop. In the routine, there is an editable textbox in which participants write a short text discussing an image component. While they’re writing in this textbox, they can click through a number of images by clicking a next and previous button, which ends the routine and starts it again with the next or previous image.

I have added a keyboard component to the routine and selected it to save ‘all keys’. However, by default, the keypresses which are saved in the csv are only the ones from the final loop of the routine before the participant terminates the loop. I want to save all the keypresses from all the loops of the routine. It doesn’t matter whether they save in one column/cell of the csv or multiple cells.

I assume I should be able to do this fairly simply by adding a snippet of code in the ‘End Routine’ tab of a code component, so all the keypresses made during that loop of the routine are saved. Would psychopy.event.getKeys() be applicable for this purpose? If so, could anyone advise me on how to actually use this piece of code and then have it save in the data file? Or if it’s not applicable, what code should I use?

Please note that it’s important that the method works online too as I will be running the study on Pavlovia.

If anyone can help I’d be very grateful! Thanks

EDIT: I should mention I’m using Psychopy Version 2022.2.5

Best wishes,
Michael