# Backspace not working on psychopy/Pavlovia

**URL:** <https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752>\
**Category:** Online experiments\
**Created:** [February 5, 2025, 2:18pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752 "2025-02-05T14:18:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ava](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@ava](https://discourse.psychopy.org/u/ava)\
**Post date:** [February 5, 2025, 2:18pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/1 "2025-02-05T14:18:48Z")

</div>

Description of the problem: I cannot delete i.e. use backspace on the entered response in text boxes. Essentially backspace is not being registered. I know people have reported this before but could not find a solution here so I’m sharing this again. I’ve tried editing the code, using an editable text box without the code component and using a keyboard reponse element but nothing is working. This is the code I had before [note: taken from a previous experiment in my lab and was working before]:  
**Begin experiment** : import string  
`allLetters = list(string.ascii_lowercase+string.digits)`  
**Begin routine** : //temporary solution to a psychopy bug

```auto
word3_2.refresh();
document.body.style.cursor='auto';
//modify = false;
**Each frame** : let theseKeys = psychoJS.eventManager.getKeys({keyList: ['a', 'b', 'c', 'd','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0','backspace','-']});

if (theseKeys.length > 0) {
  // Get the last key pressed
  let key = theseKeys[theseKeys.length - 1];

  if (key === '-') {
    key = '-'; // Handle the minus sign
  } else if (key === 'backspace') {
    // Handle Backspace: Remove the last character
    if (word3_2.text.length > 0) { //Check if there are any characters to delete
        word3_2.text = word3_2.text.slice(0, -1); // Remove the last character
    }
  } else if (key.length === 1 && (/[a-z0-9-]/.test(key))) {
    // Ensure valid characters (alphanumeric and minus) and only one character.
    word3_2.text = word3_2.text + key;
  }
}

```

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 5, 2025, 2:27pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/2 "2025-02-05T14:27:18Z")

</div>

I have two solutions to this issue. One is to use a keyboard component and count “N/A” as backspace, and the other is to use an editable textbox and count the length of the string getting shorter as backspace.

Have a look at my Key Check online demo

> [@PsychoPy Online Demos](https://discourse.psychopy.org/t/psychopy-online-demos/22319):
>
> If you have routines or experiments that are likely to be useful to others, please create a post in this thread, editing your post to add new demos so there is one post per creator. Ideally set your demos so they can be tested without consuming a credit (if you don’t have a site license) by adding a final routine that can’t end, disabling save incomplete results and adding a small number of credits. Please do not discuss the demos in this thread or create more than one post unless you have a la…

---

<div class="post-metadata">

**Author:** ![ava](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@ava](https://discourse.psychopy.org/u/ava)\
**Post date:** [February 5, 2025, 3:22pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/3 "2025-02-05T15:22:20Z")

</div>

> [@wakecarter](#):
>
> keyboard component and count “N/A” as backspace

Sorry I’m new to this. I checked the Key Check online demo and whenever I press backspace nothing is detected so I’m not sure what to make of it. Could you provide more explanation for both options you’ve mentioned here? Thanks!

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 5, 2025, 3:58pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/4 "2025-02-05T15:58:09Z")

</div>

Try the key\_resp or textbox methods

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/8/886749ebae8acef8c43c893ad457c924fce56003.png)

Here’s the code for the editable textbox method. Put a keyboard component that ends the routine on ‘return’ above the textbox.

Begin Routine

```auto
nKeys = 0
keys = ''

```

Each Frame

```auto
if len(textbox_2.text) < nKeys:
        keys = 'BACKSPACE'
        nKeys = len(textbox_2.text)
elif len(textbox_2.text) > nKeys:
        keys = textbox_2.text[-1] # Most recent key pressed
        nKeys = len(textbox_2.text)

```

---

<div class="post-metadata">

**Author:** ![ava](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@ava](https://discourse.psychopy.org/u/ava)\
**Post date:** [February 5, 2025, 4:29pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/5 "2025-02-05T16:29:59Z")

</div>

> [@wakecarter](#):
>
> Here’s the code for the editable textbox method. Put a keyboard component that ends the routine on ‘return’ above the textbox.
> 
> Begin Routine
> 
> ```auto
> nKeys = 0
> keys = ''
> 
> ```
> 
> Each Frame
> 
> ```auto
> if len(textbox_2.text) < nKeys:
> keys = 'BACKSPACE'
> nKeys = len(textbox_2.text)
> elif len(textbox_2.text) > nKeys:
> keys = textbox_2.text[-1] # Most recent key pressed
> nKeys = len(textbox_2.text)
> 
> ```

Thanks for the step-by-step guide! Unfortunately, it keeps returning Error

Unfortunately we encountered the following error:

- **ReferenceError: testRoutineBegin is not defined**

Try to run the experiment again. If the error persists, contact the experiment designer.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [February 5, 2025, 4:35pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/6 "2025-02-05T16:35:11Z")

</div>

Please could you check the developer tools for further information?

> [@Wakefield's Daily Tips](https://discourse.psychopy.org/t/wakefields-daily-tips/43371/5):
>
> Developer Tools If you encounter an error in an online study, open the Developer tools console using Ctl-Shift-I in Windows/Chrome, Cmd-Opt-J or Cmd-Opt-I in Mac/Chrome, F12 in IE/Edge, Ctrl-Shift-J in Windows/Safari or Ctrl-Opt-J in Mac/Safari. Select the Console tab at the top and errors item on the left to view errors. Ignore the 404 error related to favicon.ico Look for the error that appears on the main screen and click on the link next to Scheduler.\_currentTask. Take…

---

<div class="post-metadata">

**Author:** ![ava](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@ava](https://discourse.psychopy.org/u/ava)\
**Post date:** [February 6, 2025, 6:15am UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/7 "2025-02-06T06:15:55Z")

</div>

I have included everything you mentioned but whenever I press a key it moves onto the next screen without recording or shwoing any text. I’ve tried both checked and unchecked force end of routine settings on keyboard and modified the code.  
Begin Experiment:

```auto
key_resp = new core.Keyboard({psychoJS, clock: new util.Clock(), waitForStart: true});
typedText = ""; // Variable to store user input

```

Begin Routine:

```auto
nKeys = 0;
keys = "";
console.log("The test routine has started!");

```

Each frame:

```auto
// Ensure key_resp exists and key press is registered
if (typeof key_resp !== "undefined") {
    let newKeys = key_resp.getKeys({ waitRelease: false });
    if (newKeys.length > 0) {
        let lastKey = newKeys[newKeys.length - 1].name; // Get the last key pressed

        if (lastKey === "backspace") {
            // Handle backspace (remove last character)
            textbox_2.text = textbox_2.text.slice(0, -1);
        } else {
            // Add new key press to the text
            textbox_2.text += lastKey;
        }

        console.log("Key Pressed:", lastKey); // Debugging key presses
    }
}

```

Not sure what the issue is still!

---

<div class="post-metadata">

**Author:** ![ShinglesQueen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shinglesqueen/32/44558_2.png) [@ShinglesQueen](https://discourse.psychopy.org/u/ShinglesQueen)\
**Post date:** [March 11, 2025, 2:01am UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/8 "2025-03-11T02:01:17Z")

</div>

Hi there!  
Was this sorted out? I am currently experiencing the same issue. Also tried using a textbox and experienced the same.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 11, 2025, 7:30am UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/9 "2025-03-11T07:30:56Z")

</div>

Please could you show how you tried the textbox method?

---

<div class="post-metadata">

**Author:** ![ShinglesQueen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/shinglesqueen/32/44558_2.png) [@ShinglesQueen](https://discourse.psychopy.org/u/ShinglesQueen)\
**Post date:** [March 11, 2025, 7:49pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/10 "2025-03-11T19:49:47Z")

</div>

Sure thing! Here you go.  
I most definitely could not have done it correctly… to be clear, spacebar works fine and enter would work if i had continueRoutine = false (but for this part of exp i want it to continue and based on a time limit).

Also, I have loaded in word stimuli prior to this routine (which works fine)  
Here is the code for javascript below.

**Begin Experiment:**  
i = 0;  
n = 0;

**Begin Routine:**  
psychoJS.eventManager.clearEvents();  
target\_review.text = “”;  
written\_target.text = “”;  
displayText1 = “”;  
keysLen = 0;  
cue\_review.text = cueword[i];  
allowedKeys = [“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”];  
if ((Cond[0] === “M”)) {  
if ((typeversion[n] === “R”)) {  
target\_review.text = targetword[i];  
} else {  
if ((typeversion[n] === “T”)) {  
target\_review.text = " ";  
} else {  
if ((typeversion[n] === “E”)) {  
target\_review.text = incorrectword[i];  
}  
}  
}  
}

**Each Frame:**  
// Get currently pressed keys  
let keys = psychoJS.eventManager.getKeys();

if ((Cond[0] === “M”) && (typeversion[n] === “T”)) {  
for (let key of keys) {  
if (key === “backspace” && displayText1.length \> 0) {  
displayText1 = displayText1.slice(0, -1); // Remove last character  
} else if (key === “return”) {  
continueRoutine = true; // End trial  
} else if (key === “space”) {  
displayText1 += " "; // Add space  
} else if (key.length === 1 && allowedKeys.includes(key)) {  
displayText1 += key; // Append typed character  
}  
}

```
// Update textbox without showing any counter
written_target.text = displayText1;

```

}

**End Routine:**  
psychoJS.experiment.addData(“Trial”, typeversion[n]);  
psychoJS.experiment.addData(“CueWord”, cue\_review.text);  
psychoJS.experiment.addData(“StudyTarget”, target\_review.text);  
psychoJS.experiment.addData(“WrittenTarget”, written\_target.text);  
i += 1;  
n += 1;

reviewkeys component: set to constant, store all keys  
written\_target textbox: inside text says $displayText1 set to every frame

Thanks a bunch! 🙂

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 11, 2025, 7:52pm UTC](https://discourse.psychopy.org/t/backspace-not-working-on-psychopy-pavlovia/43752/11 "2025-03-11T19:52:43Z")

</div>

This is not the textbox method.
