Restricting allowed keys in textbox component

I am using the last version of PsychoPy (2021.1.4) for Windows and quite new to psychopy.
My goal is to create distractor task by using various mathematical questions. To do this I need to get keyboard responses from participant. I find textbox component to easiest way to do this. I have several questions regarding textbox:

  1. How to restrict the keys participants can press in textbox? I tried using keyboard component to do this but it did not work.
  2. How to limit the distractor task’s duration ? eg. 15 secs

Thank you in advance

The easiest way is to use a Keyboard :keyboard: component and a Code :code: component - in the Each Frame tab of the code component, add something like:

# what keys have been pressed this frame?
keys = keyboard.getKeys()
# if any, check that they're the right keys
if keys:
    if keys[-1] not in correctKeys:
        # if they're not valid, remove the last inputted character from the textbox
        myTextBox.text = myTextBox.text[0:-1]

Alternatively, you could set editable to be False on the textbox and use similar code to add the key if valid

Thanks a lot! I tried the solution but had attribute error, I am pasting the error below:

keys = keyboard.getKeys()
AttributeError: module ‘psychopy.hardware.keyboard’ has no attribute ‘getKeys’

Sorry, I should have been clearer, in this case keyboard should be replaced with the name of your keyboard :keyboard: component

Thanks! that had solved most of my problems!