Store only last key

Hey everyone,

My memory experiment includes a loop, where participants need to indicate which object out of a selection was memorised previously. For each trial, participants are asked to either press ‘m’ (incorrect) or ‘x’ (correct). I want all ‘m’ key presses to be stored but only the very last ‘x’ key press in case participants select more than one correct object/want to change their decision. Is there any way to write a custom code for that?

P.S. Unfortunately, I have very little coding experience so explanations would be much appreciated

To clarify, you don’t want to end the loop early but want to know the item for the last X that was pressed during the loop?

I think that to do that you would need to store the value as a variable. For example

Begin Experiment

storedValue = 0

End Routine

if key_resp.keys == 'x':
     storedValue = Item

End Experiment (or Begin Routine after the loop has finished)

thisExp.addData('StoredValue',storedValue)