How to prevent the press of the delete key (backspace) while editing the Textbox

Win 10
PsychoPy 2021.2.3

Hi everyone,
sorry to bother you but I’m desperate. I’ll try to explain my problem as much as I can.

I have a picture description task where participants are asked to write down a description in the textbox (named ‘target’). The problem is that I don’t want them to be able to delete anything they type. They can press any other key in the keyboard; “enter” is the key they use to end the routine.

Routine is composed by:

  • multiple text stimuli (words the participants need to combine to write the description)
  • keyboard (forces the end the routine when they press Enter)
  • Textbox (for the target sentence)

I’ve found on the internet some templates for the ‘code’ custom for the text stimuli and I tried to modify them multiple times to make them work for a textbox, but it doesn’t work. I don’t know if what I’m trying to do is actually doable and I’m just making some mistake in the code custom. If it is doable, how would you do it in the Builder?

Option B: I have to use a text stimulus named “text”. I tried usying a template found online in the following way:

I can write a description, but the second I press “delete”, the experiment closes with the error:
text1.text = text1.text [:0]
NameError: name ‘text1’ is not defined

But there is not “text1”, not even in the coder. So I don’t understand what’s happening.

Thank you so so much! And happy holidays

You need to delete the part of the code related to backspace slicing the input.

Coding in JS directly instead of an auto translate code component increases the chance of errors.

I think some of my demos have the code you want (Search for online demos)

@wakecarter hi! Thank you!
But is there no way to use textbox instead? “text” is giving me many problems about punctuation and capital letters.

A code with something like this:

keys = event.getKeys()
if ‘escape’ in keys:
core.quit() #so you can quit the experiment

if len(keys):
if ‘backspace’ in keys:
TextBox.text = TextBox.text
elif ‘return’ in keys:
continueRoutine = False

It doesn’t work. The experiment works but i can delete what I type in the textbox

I’m sorry, but I’m not an expert on text boxes, but I can do punctuation, capital letters and foreign characters in text components.

Try putting a keyboard component that monitors for backspace but doesn’t end the routine above your editable text box.

@wakecarter thank you! I’m trying with the text component. I used some online templates:

Finally, I can’t press the backspace key while typing. And that was my goal.
But if I press comma, period, or apostrophe, it says “comma”, “period”, “minus”. And, worst of all, if I press shift to type capital letters it says “undefined” (I’m working with German, so this is a big problem).
“Spacebar” works properly.

There are two things you need to know to correct your code.

  1. .upper needs to be manually translated to.toUpperCase for JS. Switch the code to a Both code component to make the edit after you’ve dealt with the punctuation.

  2. You already have a demo of what to do for punctuation in your line related to the space bar.

elif ‘comma’ in keys:
     text.text += ‘,’

@wakecarter thank you!
Sorry for bother you again, but I think I’m almost there.
When you say “.upper needs to be manually translated to .toUpperCase” is
else {
if (modify) {
text.text = (text.text + keys[0].toUpperCase)

Something is wrong. Sorry, this is really not cup of tea…

Comma and period work, finally! But not apostrophe

Off the top of my head there should be () after.upper and therefore also after .toUpperCase, ie toUpperCase()

For apostrophe you need

elif ‘apostrophe’ in keys:
     text.text += ‘\’’

@wakecarter THANK YOU! It worked for the capital letters.

Regarding the apostrophe, I did what you said but didn’t work and ‘minus’ showed when I pressed the apostrophe. I looked at the data csv file, and it said I pressed ‘minus’. So modified ‘apostrophe’ into ‘minus’ in the code component and, incredibly, it worked:
elif ‘minus’ in keys:
text.text = text.text + ‘\’’

JV: else {
if (_pj.in_es6(“minus”, keys)) {
text.text = (text.text + “’”);

I can’t thank you enough. You saved my experiment and my holidays.
Thank you

If you have a German keyboard you may need to make more adjustments. Check the a and z keys.

I use “Press z to continue “ to work out what keyboard is being used and adjust the code accordingly.