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?

Thanks.

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

Hello,

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: