How to add spacebar and backspace functions as a button component?

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Mac 12.2.1
PsychoPy version (e.g. 1.84.x): 2021.2.3
Standard Standalone? (y/n) If not then what?: Yes
What are you trying to achieve?:


I am trying to recreate the automated OSPAN task (Unsworth et al., 2005) and I am having a difficult time getting the “clear” and “blank” buttons to work the way I want them to work for the recall portion of this task.

Among other tasks, participants are asked to remember a set of letters in a specific order. For example, they may be asked to remember “F P N Q” and then must make button responses (using a mouse) to recall the letters they were asked to remember. Every letter they select is also present at the bottom of the screen. The screen should look something like this (mine does not have the squares next to the letters).

Screen Shot 2022-05-11 at 5.20.13 PM

If a participant only remembers “F P _ Q” and does not remember “N” the participant should have the option to add a space to help preserve the order.

If the participant initially recalled “F P Q N” and realized the “N” and “Q” are reversed, the participant should be able to delete the last two letters and re select the letters in the proper order.

In the task I am building, the letters are clickable text boxes via a mouse component. I also have three button response components participants can press if they need to add a space (blank), delete a letter (clearbutton), or move on to the next block (exit). An image is below, if it is useful.

My initial thought was simply to write a few statements to the effect “If the blank button is selected: add a space” or “If the clear button is selected: delete the previous letter”. However, I have not found a way to do this with either a code component or the Callback Function in the button components. I am using screen_text="".join(mouse.clicked_name) to collect the made responses. This is also used to display the letters at the bottom of the screen so the participant can see what they have selected. I tried mouse.clicked_name in a few if statements, but I am not sure how to call the spacebar or backspace button. I tried the statement below as a code component under the Every Frame tab, but “keys” is not defined. I assume because this is specifically for a keyboard response, but I am not sure of an alternative.

if mouse.clicked_name == 'blank': keys.SPACE = 1
if mouse.clicked_name == 'clearbutton': keys.BACKSPACE = 1

I am sure the answer is relatively straight forward, but I have not been successful in coming up with a solution. Any thoughts?


Hi, does your task need to have clickable letters? Because if you use the textbox component and check the Editable? field, you can easily have the space and backspace functions.

@suelynnmah, I appreciate the response!

Yeah, I tried this as a possible alternative. I am able to maintain the use of clickable letters, however, I was still unable to resolve the issue of adding a space or backspace because how I am displaying which letter was clicked was independent of the edibility afforded by the textbox component. That is my understanding at least. Do you think I am missing something? We are trying to stick as closely to the original paradigm as possible which is why were are hesitant to change how the letters are recalled.