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