How to prevent a loop from pressing backspace in a typed response

I managed to get every thing right following this tutorial

I run it, it works fine until I pressed backspace twice. it gets me into a loop of having the last letter pressed appeared (which removed by the first click on backspace). on a third click on a backspace the word “backspace” appeared and on trying to end this by pressing Enter it doesn’t work.

This is the code I enter in Each Frame:

if(“backspace” in key_resp_task4.keys):

if(len(key_resp_task4.keys) > 0):

elif(“return” in key_resp_task4.keys):

if(len(key_resp_task4.keys) > 3):
   screen_text1 = ''.join(key_resp_task4.keys)
   thisExp.addData("recall_resp4", screen_text1)

   continueRoutine = False

screen_text1 = ‘’.join(key_resp_task4.keys)

Any one encounter such problem. My guessing are it is something related to the code pop() and that a code to end the loop is needed!

I also get this error when I run it online

  • TypeError: Cannot use ‘in’ operator to search for ‘backspace’ in undefined


if you read the comments you’ll notice that your are not the only person for which this approach does not work. See this for an alternative approach.

Best wishes Jens

1 Like

Thanks Jens

I’ve followed the way of coding in the suggested link. As I can see in this method, there is no need to add a keyboard as in the case of the old method. However, when I run it in the builder and tried to type the response, the typed text cannot be seen. This is the case in the builder.

I tried also to run it online and encountered this error code message:

  • TypeError: Cannot read property ‘clearEvents’ of undefined

Any idea what’s the reason and how to solve it?


When you run the experiment online, you need to add some lines JavaScript, see here.

Create a JavaScript only code-element at the beginning of your experiment and add, at least, the following lines (you find an explanation following the link above). See the attached experiment below for a similar code-element, codeJS, in the first routine, startExp, of the experiment

shuffle = util.shuffle;
Array.prototype.append = [].push;

Well, if you don’t see your type input when running locally, your code is not yet correct :wink:

Take a look at the attached experiment. You can ignore most of the experiment. It is change-blindness experiment in German. The relevant routine is called “BerichtChangBlindExp3.psyexp (47.5 KB). codeAntwort is the code-element used to register the typed answer and antwort the text-element to display the response. Be aware that some non-English keys might not work.

Cheers Jens

1 Like

Thank you so much, it worked well now. Do you know how to limit pressing “return” unless after typing at least 3 letters?


you need to check for the length of inputText. So something around this line should do the job in python:

if actualKeys[i] == 'return' and len(inputText > 2):

Now, the routine only ends if the length of inputText is larger than 2 and return is pressed.

Cheers Jens

1 Like

Thank you so much Jens.