psychopy.org | Reference | Downloads | Github

getKeys - how to use to capture time of first keypress

The piece of code below is used for people to type in their responses (borrowed from one of the pilot demos). It’s in an ‘Each Frame’ element

I’ve added the line 'thisExp.addData(‘word_complete_time’, t) in order to get the time at which the participant hits ‘Enter’. However, I’m having difficulty in putting in some code that works to capture the time at which the participant started typing (i.e. I’m experiencing the issue that every time the Frame loops around it constantly updates any keyboard response timings to the next letter and the letter after that etc etc). Any ideas on what I should add would be appreciated thanks.


keys = event.getKeys()

if len(keys):
    if 'space' in keys:
        text.text = text.text + ' '
    elif 'backspace' in keys:
        text.text = text.text[:-1]
    elif 'lshift' in keys or 'rshift' in keys:
        modify = True
    elif 'return' in keys:
        thisExp.addData('word_complete_time', t)
        continueRoutine = False
    else:
        if modify:
            text.text = text.text + keys[0].upper()
            modify = False
        else:
            text.text = text.text + keys[0]

The first bit of advice I’d give is to switch out your Text component for the new Textbox component in 2020.2! It allows for an editable text field in a way which is much cleaner and more intuitive than the Text component. If you do this, then you could add some code to check EachFrame whether myTextbox.text.endswith('\n'), i.e. “was the last character typed into my textbox a new line character (ENTER)” to check for the first Enter (and end the routine, if that’s what you want to do). You could similarly check for when they started typing by either checking the length of myTextbox.text or comparing it to whatever your starting value was.

Thank you @TParsons , I’ll download the new version and look at using the Textbox component.

I’m having a very difficult time with a similar task. Any chance you could post the full code for this?