psychopy.org | Reference | Downloads | Github

Text Box Not Working Correctly when Attempting Restictions in Content

OS : Windows 7
PsychoPy version : 1.90.2

What are you trying to achieve?:
Hello everyone,

I am attempting to create the automatic BART (Pleskac et al., 2008). To do so, I created a text input box per the following video: https://www.youtube.com/watch?v=gqBNAFfvDqY

The input box works correctly. However, I need the routine to continue when pressing Enter until a number below 129 has been typed in the text box. I have been able to restrict the Enter button when typing a number larger than 3 digits, but I need 3 digit numbers below 129.

Here is the code I have so far that works with text below 999 inside a Code component:

Begin Experiment

inputText = ""

Begin Routine

theseKeys=""
shift_flag = False
InputText.alignHoriz = 'center'

Each Frame

n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return' and len(inputText) > 0 and len(inputText) < 4:
        # pressing RETURN means time to stop
        continueRoutine = False
        break

    elif theseKeys[i] == 'backspace':
        inputText = inputText[:-1]  # lose the final character
        i = i + 1

    elif theseKeys[i] == 'space':
        inputText += ' '
        i = i + 1

    elif theseKeys[i] in ['lshift', 'rshift']:
        shift_flag = True
        i = i + 1
    elif theseKeys[i] == 'period':
        inputText = inputText + "." 
        i = i + 1

    else:
        if len(theseKeys[i]) == 1:
            # we only have 1 char so should be a normal key, 
            # otherwise it might be 'ctrl' or similar so ignore it
            if shift_flag:
                inputText += chr( ord(theseKeys[i]) - ord(' '))
                shift_flag = False
            else:
                inputText += theseKeys[i]

        i = i + 1

End Routine

inputText=""

What did you try to make it work?:
I have tried referencing a specific character in the strings created by the variables inputText and theseKeys, but it doesn’t seem to work. Here is what I have tried:

Each Frame

n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return' and len(inputText) > 3:
        # pressing RETURN means time to stop
        continueRoutine = True
        break

    elif theseKeys[i] == 'return' and len(inputText) > 2 and inputText[0] == '1':
        continueRoutine = False
        break

    elif theseKeys[i] == 'return' and len(inputText) > 0:
        continueRoutine = False

        break

Using the above code does not restrict numbers above 199.

I’ve tried the following code as well:

n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return' and len(inputText) > 3:
        # pressing RETURN means time to stop
        continueRoutine = True

    elif theseKeys[i] == 'return' and len(inputText) > 2 and inputText[0] != '1':
        continueRoutine = True

    elif theseKeys[i] == 'return' and len(inputText) < 3:
        continueRoutine = False

        break

After typing a 3 digit number and pressing Enter it causes psychopy to freeze and crash, however no error message appears.

I’ve tried other variations of referencing the first character in the string of the variables inputText and theseKeys by trying: inputText[0] or theseKeys[0] and telling the routine to continue until the first number of a 3 digit number is a 1 or 0, however, it doesn’t work and pressing Enter after typing a number above 199 still works.

What specifically went wrong when you tried that?:
When I’ve tried using “[0]” to reference the first character of the string and telling to routine to continue until it is a “0” or “1”, the routine still ends when typing a 3 digit number above 199.

If anyone has any suggestions, it would be greatly appreciated.

Thanks,
Erick