How to end a routine with a button only after pressing at least >1 key in the editable textbox

Hello, I would like to add to my experiment a “mandatory” textbox. In my routine participants see a photo followed by the possibility to give three answers (editabile textboxs), in order to end the routine and see the next photo, they need to press the “avanti” key (next). I wanted to make at least one textbox mandatory by tellling Psychopy to disable the button if there are 0 keys or < x keys pressed. I used the code below but it does not work (even though it does not show me an error message).

OS (e.g. Wind10): Windows 10
PsychoPy version (e.g. 1.84.x): v.2022.2.5
What did you try to make it work?:
keys = event.getKeys()

Check if the participant has typed anything

text_input = risposta_prima1.text
if len(text_input) == 0:
# If the participant has not typed anything, disable the next button
avanti1.enabled = False

What specifically went wrong when you tried that?:
It doesn’t give me any errorr message but the code doesn’t work either, if I run the experiment the “next” button (to end the routine) works even if I don’t press any keys.
I also tried with “if len(text_input) < 1” or “if len(text_input) < 2” but no success either. Has anyone an idea on what goes wrong or if there are other solutions to realize the mandatory textbox or costumized end routine after x key presses? Thank you very much!