Restricting Keys When Using textBox?

The experiment I am working on requires participants to respond to a string that they are presented with. In this particular section, I needed the response and the string on the same screen, so I am using the editable textBox component. It works, but I’m having trouble with two things:

  1. I have two keyboard components. The first is supposed to restrict the keys to G,N, and backspace for the participant response but the doesn’t seem to be working with the editable textbox. All the keys seem to be allowed despite that. Is there a way to get the restriction to work or does that need to be done in a separate code?
  2. The second keyboard component makes ‘enter’ an allowed key for the purpose of ending the routine, but when I press enter it just makes a new line in the textBox rather than moving on to the next string.

Any help with this would be appreciated, thank you!

When there’s an editable Textbox on screen, keyboard input goes to that before the Keyboard components. When you place restrictions on a Keyboard component, those restrictions apply specifically to that component, which is why it’s not limiting keypresses to the Textbox.

In general we recommend using a Mouse component to move on, as this won’t conflict with a Textbox. As for disabling all keys but G and N, you could use a Code component to remove any characters but G and N each frame. It would look something like this:

textbox.text = "".join(ch for ch in textbox.text if ch.lower in ["g", "n", "backspace"])

I placed in a Mouse component and that works well, but the Code component is reading a syntax error. @TParsons

For a list comprehension like this, I think you will need to surround it with square brackets, e.g.

[ch.upper() for ch in textbox.text if ch.lower() in ["g", "n", "backspace"]]

Also note that .lower() and .upper() are functions, so they need round brackets.

I tried this, both with and without square brackets, but it still says that there is an error.

Now you are missing the round brackets required by the .join() function.

NB Posting screenshots isn’t really useful here: we can’t see the full line of code, or copy and paste it with the edits required. Please just paste in the actual text of the code.

I have a similar problem: I’d like to limit keys for a texboxCode to only numbers.

I tries to put
textboxCode.text = ‘’.join([ch.upper() for ch in textboxCode.text if ch.lower() in [‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘0’, ‘return’, ‘backspace’, ‘num_1’, ‘num_2’, ‘num_3’,‘num_4’,‘num_5’,‘num_6’,‘num_7’,‘num_8’,‘num_9’,‘num_0’]])
to the code component each frame tab but the experiment crashes when I try to run it in Pavlovia.

Am I doing something wrong?


Hi demameba,
Did you find a solution?
I have the same problem.


code is really difficult to read if you don’t surround it by triple `. Anyway, the syntax of join differ between python and javscript. You need to edit the javascript manually to get it to work on Pavalovia. See PsychoPy Python to Javascript crib sheet for differences between PsychoPy and PsychJS.

Best wishes Jens

Hi ayag,
didn’t solve it at the time so i just let all the entries allowed. However I have not returned back to the issue yet and checked for later versions though.

Thanks for answering :slight_smile:


Apologies for reviving this old post but I have a similar question when running my exp online.
@TParsons snippet almost worked in my case (and it was auto-translated into JS almost fine). I have one issue though:

I want to restrict responses to numbers 0-9 i.e. using the top row of numbers on a standard keyboard and not the numerical keypad. Typically this is easy with the allowed keys in the keyboard component i.e. entering the numbers 0-9 which are not the same as num_0 - num_9. The snippet though (or the textbox component) will include responses entered by the numerical keypad. So if a P responds 2 either using the top rows of numbers or using the numerical keypad this will translate into “2”. Is there a way to exclude numerical keypad responses?

I already have a keyboard component (called report) and I was thinking of calling the allowed keys list, something like the below and see if this works but I cannot find a way to call this list.

textbox.text = "".join(ch for ch in textbox.text if ch in report.allowedKeys)

Thanks in advance for any tips.


OK I got it. If anyone else with a similar question to mine visits this thread, solution below which is very simple.
Given that you have already a keyboard component with allowed keys (in my case the component is called “report”) and a textbox component (in my case called “textbox”):

// Python snippet
if report.keys:
    textbox.text = "".join(report.keys)
    textbox.text = []

// JS snippet
if (report.keys) {
    textbox.text = report.keys.join("");
} else {
    textbox.text = [];