I am attempting to creeate a function that takes in participant ID (using numbers), allows “Backspace” to delete keys, and terminates after the participant presses “Enter”. I have done this successfully in Psychopy, however, am having some difficulty implementing it as a code, separately from the Builder.
The code below terminates after a single entry is made. I tried changing the “if statement” to a “while loop” and the keeps displaying the same number with every flip.
I was wondering how to make this display every number that a participant enters immediately and terminate after “Enter” is pressed.
# Get participant ID def getID(): message = visual.TextStim(win, text=str('Please enter your ID and press Enter once you are done:'), pos=(0.0, 0.25), height=0.04) message.draw() win.flip() text='' event.clearEvents('keyboard') keys = event.waitKeys(keyList=['1','2','3','4','5','6','7','8','9','0', 'return', 'backspace']) if 'return' not in keys: if len(keys): if 'backspace' in keys: text = text[:-1] else: text = text + keys ID = visual.TextStim(win, text=text, pos=(0.0, 0.0), height=0.04) message.draw() ID.draw() win.flip() if 'return' in keys: return ID ID= getID()