How to store reaction time data for each key press using textbox component

OS: MacOS Big Sur 11.1
PsychoPy version (e.g. 1.84.x): 2020.2.1
Standard Standalone? (y/n): Yes
What are you trying to achieve?: I am trying to create an experiment in which participants see a word on the screen which they then have to type. I need them to be able to see what they’re typing but I also want to record reaction times for each key press and store which keys were pressed, including backspace. Once participants have finished typing the word, I want the ‘return’ key to end the routine.

What did you try to make it work?: I have tried adding both keyboard and textbox components, with the textbox component reading from an .xcl file to show the words to participants that they must type. The keyboard component does not force the end of the routine, all keys are allowed (i.e. none specified in this box) and all keys are stored. I added this component hoping it would get and store reaction time data for each key press. To end the routine I have added a code component to every frame as follows:

if len(Keyboard.keys) > 0:
    if Keyboard.keys[len(Keyboard.keys)-1] == 'return':
        continueRoutine = False

What specifically went wrong when you tried that?:
When I run the experiment just a blank grey background screen is shown and none of the words are displayed. When I just have the textbox component, the words are displayed correctly, so my thinking is there is some kind of issue with the interaction between the textbox and keyboard/code components, but I just can’t figure out what’s wrong!

Any help would be hugely appreciated.

Hi! I made this task in the builder myself. Can you attach a picture of your builder for this routine? I had it set up such that the words I wanted shown were in a column in an excel conditions file uploaded to the loop. Then I set the text display to pull the text from that column. If you set that text to repeat every repeat, you should get different words to show up every time the loop initiates.

Then I set a textbox for the participant response and a keyboard component to end the return.

:textbox: Textbox components have an attribute called .onTextCallback which can be set to be a function, this function is called whenever a character is added to the textbox. So you could define a function which adds the time to a list and then store this list at the end of the routine. You’d do this in a :code: Code component like so:

Begin routine

# Create a list to store keypress times
keyPressTimes = []
# Define function to add to this list
def storeTime():
    # Pull in variables from outside this function
    global keyPressTimes, t
    # Add current time to list of keypress times
    keyPressTimes.append(t)
# Assign keypress function to the textbox
myTextbox.onTextCallback = storeTime

End routine

# Store key press times
thisExp.addData("keyPressTimes", keyPressTimes)
2 Likes

Hello, do you think this would work online? I am trying to store RT data for each keypress in a textbox component in an online experiment. Thanks!

Just put a keyboard response collecting all key presses that doesn’t end the routine above the text box. I’ve just tested with text-box [PsychoPy] and got data like this:

trial.started	trial.stopped	key_resp.keys	key_resp.rt	key_resp.duration	textbox.text	participant	session	date	expName	psychopyVersion	OS	frameRate
0.0007	13.8778	["lshift","o","n","e","space","t","w","o","space","t","h","r","e","e","space","f","o","u","r","space","a","n","d","space","f","i","v","e","s","return"]	[3.4391999999992544,3.6564999999999994,3.906300000000745,4.075800000000744,4.21010000000149,4.87210000000149,5.19060000000149,5.2780000000000005,5.422100000001489,6.190400000002235,6.374499999999999,6.438400000002234,6.805800000000744,7.008100000001489,7.118699999999254,7.5405,7.735400000002235,8.609199999999255,8.878,10.238999999999999,10.839199999999254,10.938400000002234,11.040199999999254,11.15510000000149,12.478199999999255,12.62260000000149,12.710199999999254,12.838800000000745,13.055400000002233,13.838500000000002]	[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]	One two three four and fives	821029	1	2023-09-12_20h42.51.522	text-box	2023.2.2	Win32	60.04563468

Thank you! This worked perfectly.