xkcd image left

Resources

GitHub

OSF

I build my experiments using PsychoPy, an open-source Python-based application. Occasionally I post snippets of useful code for non-built-in functions - feel free to use them:

Collecting keyboard input and mirroring onscreen

One built-in function I missed from E-Prime was the ability to collect text input from the user and show it in the experiment window as they are typing it. This simple block of code will do that - just adjust the position of the TextStim to suit you. This code collects the input in the variable text, which you can then store in your output/write to a file as needed.

text=''
#until return pressed, listen for letter keys & add to text string
while event.getKeys(keyList=['return'])==[]:
    letterlist=event.getKeys(keyList=['q','w','e','r','t','y','u','i','o','p','a','s','d','f',
	'g','h','j','k','l','z','x','c','v','b','n','m','backspace'])
    for l in letterlist:
        #if key isn't backspace, add key pressed to the string
        if l !='backspace':
	    text+=l
        #otherwise, take the last letter off the string
        elif len(text)>0:
	    text=text[:-1]
    #continually redraw text onscreen until return pressed
    response = visual.TextStim(win, text=text,color="black",pos=(0,-100))
    response.draw()
    win.flip()
event.clearEvents()
xkcd image right