How to print keyboard press response time

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Mac Sequoia 15.5
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2025.1.1, using builder mode
Standard Standalone Installation? (y/n) If not then what?: yes I think so
Do you want it to also run online? (y/n) not needed, however start up is very slow locally, so all troubleshooting has been done online.
What are you trying to achieve?: I am attempting to create an experiment that logs the time elapsed for a series of tasks performed by the participant. Only the experimenter will interact with the experiment, and they will press a key to start and stop the timer for each task. Then, the duration of each task will be printed to the log file. Some tasks will have multiple ā€œlapsā€ where I would need to keep track of the timestamp of multiple button presses.

Currently my experiment is set up so each task has 3 routines: pre test with the name of the task, during the task where the timer is active, and a post task where I would like to display the time elapsed during the task before moving to the next task.

I have two challenges. The most simple is how to get the time elapsed during the task to show on the screen of the post-task routine. I am able to successfully export the duration of this task as keyboardModuleName.rt in the log file. I’ve tried creating a text object in the post-task routine with text print (ā€œkeboardModuleName.rtā€) and I’ve also tried a code component inserted at the top of the list of commands in the routine with print(f"Time elapsed for this routine: {t} seconds") entered into the ā€œEnd Routineā€ tab.

My second challenge would be repeating the above but for a task that has multiple keyboard presses for mutliple laps. I would like to print the time of each lap to the screen as it occurs.

print prints to the stdout tab of the runner (or the developer tools console online.

To print to the screen you need to either put a variable in the text component or setting the text component in a code component.

Thank you! This works.

Any ideas on how to print multiple outputs to the screen over time? My other test has multiple laps we need to collect timestamps for. I have the test set up to loop through the laps, and I do successfully record the length of time of each lap in in log file, but I would also like the stop times of each lap to appear on the screen for my experimenter to have as a reference. I have tried using the variable $lapped_routine.stopped in the text field, but I get the error ā€œlapped_routine is not definedā€ when I torubleshoot the experiment and the experiment stops. I assume this has something to do with my loop but I still am having difficulty figuring it out.

i have some screen shots attached. one shows my flow with the routine in question highlighted with the red arrow. The second shows the layout of the routine, with the module that creates the timestamp highlighted by the blue arrow and the module of code shown in image 3 highlighted by the red arrow. Image 3 has my code.

Hello @SMM

you can print a variable to the console with the following print-command

print("_40mwt_lapend: ", _40mwt_lapend)

I do not see a routine called lapped_routine in your flow.

I noticed in the flow that you created a couple of routines with similar names, e.g. routine_4MWT1 and routine_40MWT1. It is better to reuse a routine if only the stimuli and some presentation parameters differ from one routine to another. Therefore, instead of using constants, use variables where necessary. See here Wakefield's Daily Tips - #7 by wakecarter

Best wishes Jens

Hi Jens!

Thank you so much for your reply! Unfortunately, I am having trouble getting that solution to work.

After troubleshooting some more, I believe the issue is how I am defining _40mwt_lapend. When I remove that line, the script runs. Is there a specific syntax I should use when subtracting two variables? Or maybe an issue with where the code is placed?

Thank you!

.stopped is not directly available during the experiment

I would recommend creating bespoke timers with e.g. myClock = core.Clock() and then reading them with myClock.getTime() and resetting them with myClock.reset()

Thanks for the help, and apologies for my delay, I wasn’t getting anywhere and needed to step away from this project for a bit.

I’ve tried to start from scratch to see if I can better understand how to implement the bespoke timers you had mentioned. Unfortunately I haven’t made much progress. I’ve used the resources on your google doc here to the best of my ability, trying to integrate them in a simpler experiment with hopefully less to complicate and mess things up. However, I am still running into issues when I attempt to run the experiment.
I get the following error: ā€œUnboundLocalError: local variable ā€˜core’ referenced before assignmentā€
I’ve tried moving the lines:

import psychopy.core as core
import psychopy.event as event

From the ā€œbegin routineā€ pane to the ā€œbefore experimentā€ pane of my code snippet, but I am still having trouble. In addition, the script takes nearly 10 minutes to fail and generate an error (if it even starts at all), which I assume has something to do with what I am doing wrong but this makes debugging even more challenging.

I have the file attached as I think that make be an easier way to determine the issue. If you have a moment, could you take a look? I really appreciate your quick responses and have used your responses to others’ questions for help with this as well, unfortunately I just can’t figure this out. I am also open to suggestions for other resources to improve my skills. So far, I haven’t been able to find the solution to my problem in the psychopy doc, other discourse convos, etc likely because I am so new at this and need more foundational information.

Edit: Just a bit more context about the issue with the script’s delayed start- I downloaded another script that includes a timer from a previous post you had helped with and this loads and runs in a reasonable amount of time.

Timer_test_Jan2.psyexp (17.5 KB)

Here’s one of my tips to dissuade you from over-importing.

Hello @SMM

I edited your experiment such that it at least runs. It might not run in the way you intended. It is not clear to me what the start and stop-keypressed should do.

Timer_test_Jan2.psyexp (17.0 KB)

Notice, that I still load the Python-Library math to use the function floor.

Best wishes Jens

Hi Jens.

Thank you very much. The goal of the project is to record the amount of time it takes a participant to perform a test (not on the computer). Just a simple stopwatch. The reason to use Psychopy for this and not an actual stopwatch is that I need to integrate LSL to sync event codes to a recording software we will run during the experiment.

Anyway, I have made progress and can now display the time it took for the participant to complete each lap, but I would like the time elapsed since starting lap one as well just to make things more clear for my research assistants. I am using the ā€œtimeā€ and ā€œloggingā€ library.

The task has 10 timed laps. I have 1 routine that is the ā€œlapstartā€ and one that is ā€œlapendā€. I have a loop for ā€œlapendā€ that gives me the 9 additional laps. Within lap end, Begin Routine I have:

curlap=curlap+1
testName = f'40 meter walk test 1'
MyMess=f"Current Lap {curlap}\n"

for idx in range(0,curlap-1):
    lasdur=EndTime[idx]-StartTime[idx]
    MyMess=MyMess + f"Lap {idx+1} duration {lasdur}\n"

MyMess=MyMess+"\n Press SpaceBar"

Under ā€œeach frameā€ I have:

mt = time.time()
CurrTime = round(mt,2)
showTime = CurrTime-StartTime[curlap]
timeMess = f"Time elapsed: {CurrTime - StartTime[curlap]}\n"

And finally, under end routine:

tt=time.time()
EndTime.append(tt)
StartTime.append(tt)

logmss=f"Lap {curlap}: {EndTime[curlap-1]-StartTime[curlap-1]}"
logging.log(logmss,level=logging.DATA)

My issue is with how I am defining timeMess/showTime in ā€˜each frame’. I get an error that states:
showTime = CurrTime-StartTime[curlap]
IndexError: list index out of range

To try to trouble shoot I have printed CurrTime and StartTime and it seems like CurrTime is always equal to or greater than StartTime, so I am unsure why it would be out of range. I’m sure this is an issue of me not understanding indexing or something similar within python but I can’t figure it out.

Hello @SMM

What is the value of the index curlap at that moment? Perhaps you just have to wait one frame?

Best wishes jens

Hi Jens,

Thank you, I will take a look. Is there an easy way to test the value of curlap at different moments in the experiment? Or would i have to set curlap manually and rerun the experiment with different values to assess this?

1 Like