psychopy.org | Reference | Downloads | Github

Participant Input using code

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[0]
        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()

I think the issue is that you are using event.waitKeys(), instead of event.getKeys().
This might give you the code you need: https://gitlab.pavlovia.org/demos/textinput/

Alternatively, you can code a GUI to get that information before the experiment runs, for instance:

expName = 'Experiment 2-01'
expInfo = {'Participant':'', 'session':'w2020'}
dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False,title=expName)
if dlg.OK == False:
    core.quit()
expInfo['date'] = data.getDateStr()

Note that this will create a small box onscreen where the participant can fill out their info, then they can start the experiment by pressing ‘enter’ or with a button click.


I would put the GUI code after you set your directory, but before you define the window.

Hope this helps!

1 Like

Sorry for the double post, but if you use the first method I listed, you may run into trouble with punctuation. Here is a post that provides a workaround for that: Typed response: punctuation

1 Like

Thank you for this information!
I was able to implement it, but wondering if there is a way to “require” an ID before pressing “okay”? As is, participants can simply click “OK” and leave the ID blank.

Hmm… I’m not sure about that one! I wonder whether you can check the length of that item in the dictionary and use an ‘if’ statement to prevent them from moving on if there is nothing in that field? Maybe you could set up another dialogue box to show() if the user tries to continue while the participant ID is empty.
I don’t know to what extent the GUIs are customizable though to accommodate required responses etc.