psychopy.org | Reference | Downloads | Github

List appears not to be reliably popped when using pop method?

I have the following JS code in an EachFrame tab. A text and a keyboard component.

This code structure works in python. I’m working in v2020.1.2

I put in a check to avoid errors if there are no digits in the list to pop.

The idea is that when a backspace is entered it and the previous character in the list will be removed and the session will end when a “return” is entered

if ((key_resp.keys.length > 0)) {
    if ((key_resp.keys.slice((- 1))[0] === "backspace")) {
        key_resp.keys.pop();
        if ((key_resp.keys.length > 0)) {
            key_resp.keys.pop();
        }
    } else {
        if ((key_resp.keys.slice((- 1))[0] === "return")) {
            key_resp.keys.pop();
            continueRoutine = false;
        }
    }
}

recallListText.text = key_resp.keys.join('');

On piloting the code and entering digits sequentially on the keyboard, I get the following sequence displayed in the text component.

keypress   display
1          1
2          12
3          123
4          1234
backspace  123
5          1234backspace5

So the code works as desired until the first entry after the backspace when it reintroduces the ‘deleted’ 4 and backspace in front of the latest entry.
It appears that the pops are working to perform the required deletion but somehow 'deleted characters get reintroduced by the entry of the latest key. I am confused because the key_resp.keys list must be being operated on correctly in the first instance in order to remove the required characters from the text display when I press backspace, so where are the 'deleted characters reappearing from.

Hope its a simple oversight on my part, but I cant seem to get over this hurdle.

Thanks for any advice.

If I were you I would add and delete characters from a parallel array rather than the PsychoPy defined .keys

I suspect that when you alter .keys you are altering a temporary copy of it, but the next time PsychoPy accesses the array it is refreshed to the full list of keys pressed.