Bug in Textbox Beta; allowed keys sometimes don't react. need help

Dear community

I’m new on PsychoPy (v2021.2.3) and have some troubles with Textbox and/or coder.

In my experiment there is a Routine with Text (for instructions), a Textbox Beta (to type an answer), a coder (to define allowed keys) and a keyboard (to end the routine by press “space”)

Participants have to guess how many seconds they have seen a polygon before and should type numbers and even decimal places. That’s why I can’t use the keyboard only because it would force the end of routine. So I found a code in a forum to define multiple allowed keys for Textbox:

each Frame:

textboxvp.text = “”.join([ch.upper() for ch in textboxvp.text if ch.lower() in [“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“0”,"."]])

The experiment works but sometimes keys only react after pressing several random numbers. e.g if I want to type “4” the number 4 does not react and I have first to press several other numbers until one appear.

Unfortunately I can’t see any pattern why it sometimes works proper and sometimes not and it seems totally random which numbers I have to press until any appear. Same problem also on other Laptops, not only on mine (macOS Big Sur).

I would be very grateful for any advice.
Thank you!

Hello,

you could use code only to register the response.
Begin routine tab

text.text = ''
event.clearEvents('keyboard')

Each frame

keys = event.getKeys()

if len(keys):
    if 'backspace' in keys and len(text.text):
        text.text = text.text[:-1]
    elif ('return' in keys or 'num_enter' in keys) and text.text != '':
        continueRoutine = False
    elif 'minus' in keys or '-' in keys or 'num_subtract' in keys or 'Minus' in keys or 'slash' in keys:
        text.text = text.text+'-'
    elif '1' in keys or 'num_1' in keys:
        text.text = text.text+'1'
    elif '2' in keys or 'num_2' in keys:
        text.text = text.text+'2'
    elif '3' in keys or 'num_3' in keys:
        text.text = text.text+'3'
    elif '4' in keys or 'num_4' in keys:
        text.text = text.text+'4'
    elif '5' in keys or 'num_5' in keys:
        text.text = text.text+'5'
    elif '6' in keys or 'num_6' in keys:
        text.text = text.text+'6'
    elif '7' in keys or 'num_7' in keys:
        text.text = text.text+'7'
    elif '8' in keys or 'num_8' in keys:
        text.text = text.text+'8'
    elif '9' in keys or 'num_9' in keys:
        text.text = text.text+'9'
    elif '0' in keys or 'num_0' in keys:
        text.text = text.text+'0'

End routine tab

thisExp.addData("Antwort", text.text)

grafik

Best wishes Jens

Thank you. I work now only with the builder “text” and not “textbBox” anymore.