psychopy.org | Reference | Downloads | Github

Show keyboard response on screen using code and keyboard components

TL/DR: Help me make a code with the code component using the keyboard component that will put participants’ responses on the screen with backspace and spaces.

I’m trying to using the coding component with the keyboard component to get participants’ responses to display on the screen. I also want them to be able to backspace and enter spaces. I was originally using the code from this Youtube tutorial: https://www.youtube.com/watch?v=RlA1qW37huw. His code looks something like this (I shortened it to show which parts of it I’m trying to use):

Key:
keyName = name of my keyboard
text_name = name of the text that goes in the text component
All in the “Each Frame” tab

if 'backspace' in keyName.keys :
....keyName.keys.remove('backspace')
....
....if len(keyName.keys) > 0 :
........keyName.keys.pop()

elif 'space' in keyName.keys
....keyName.keys.remove('space')
....keyName.keys.append(' ')

text_name = ''.join(keyName.keys)

But that only works for maybe the first backspace (sometimes) and then it’ll write ‘backspace’ up on the screen. According to comments on the video, his code started doing this after a PsychoPy update. So I’ve tried about a million ways to fix this, but I can’t figure it out. The only help I can find on the internet says to input keyboard hardware, which I’m trying to avoid for timing purposes. I don’t even know what else I can call from the keyboard component other than .keys. I’m currently trying to make a separate list from keyName.keys and then add it to another list one at a time, but some variations I’ve tried makes lists with a million of each letter, and others are constantly replacing the letter that came before. I’m just going to copy/paste the code I currently have as is so you can see some of the millions of variations that I’ve tried (commented out).

keyList = keyName.keys

if len(keyList) > 0 :

    #keys2 = keyList
    
    #keys2 = ''.join(keyList)
    #currentKey = keyList[-1]
    
    if keyList[-1] == 'backspace' : #in keysJoined : #currentKey == 'backspace' :
        
        if len(keys2) > 0 :
            
            del keys2[nameLength]
            nameLength = nameLength - 1
        
        #keys2 = keyList.split('backspace')
        
        #keys2.remove('backspace')
        #keys2.remove(keys2[-1])
        #keys2 = keysJoined[:-1]
        #keys2 = keys2[:-1]
        
    elif keyList[-1] == 'space' : #in keysJoined : #currentKey == 'space' :
        
        keys2[nameLength] = ' '
        nameLength = nameLength + 1
        
        #keys2.append(' ')
        
        #keys2.remove('space')
        #keys2.append(' ')
        #keys2 = ''.join([keys2,' '])
        
    else :
        
        keys2[nameLength] = keyList[-1]
        nameLength = nameLength + 1
        
        #keys2.append(keyList[-1])
        
        #keys2.append(keysJoined[-1])
        #keys2 = ''.join([keys2,currentKey])

text_name = ''.join(keys2)

#keys2 = ''.join(keyList)
#text_name = keys2

So yeah, as you can see, I’ve tried a bunch of things and can’t figure it out. Please help. <3

It looks like you’re trying to manually build something which we now have a specialised component for! Have you looked at the new Textbox component in the latest versions? That should do exactly what you’re looking for here - allow participants to type and have what they type visible onscreen

Haha yes, I noticed that there was an update with textboxes a day or so later… I spent way too long trying to figure this out before noticing. I guess I should’ve updated the post. Thanks for responding! :woman_facepalming:t4: