Backspace not working on psychopy/Pavlovia

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

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;
  }
}

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

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!

Try the key_resp or textbox methods

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

Begin Routine

nKeys = 0
keys = ''

Each Frame

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.

Please could you check the developer tools for further information?

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:

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

Begin Routine:

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

Each frame:

// 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!

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

Please could you show how you tried the textbox method?

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! :slight_smile:

This is not the textbox method.