Re: [psychopy-users] Printing keyboard input to the screen

1,257 views
Skip to first unread message

Jeremy Gray

unread,
Aug 15, 2012, 2:53:52 PM8/15/12
to psychop...@googlegroups.com
Hi Fran,

In brief, there's not a built-in way to do this. But, since you are
using the coder, it should be pretty easy to do something close. Just
have to have a loop in which you listen for individual key presses,
using event.getKeys or event.waitKeys(), and when a key is detected,
append it to a string (or remove the last character, if backspace was
pressed, or end the trial if return was pressed, for example--you'll
need to add the logic for the various cases). then set the text of a
TextStim to the new string, then draw() the TextStim and finally
win.flip() to display the new string. (you will also need to redraw
other things that you want to display, such as the object, if you
like). This way the string will be displayed and will grow in both
directions, right and left from the center point. Then save the string
when the loop is exited.

--Jeremy

On Wed, Aug 15, 2012 at 2:43 PM, Fran <f.kon...@gmail.com> wrote:
> Hello,
>
> I'm planning an experiment where participants have to recognise objects. As
> soon as they do so, they have to press a key and then enter the name of the
> object. I would like to display whatever they're typing directly on the
> screen so the participant can see it; additionally the input has to be
> recorded as a string so it can be compared to names in a database later on.
>
> My problem is that I'm trying to find a way how to record their input and
> display it directly on the screen - I suppose I could use event.getKeys, but
> that only records one keypress at a time. Is there an elegant way of doing
> this? I've looked at the demos and at the documentation online, but haven't
> been able to find anything.
>
> I use the coder version of PsychoPy, by the way.
>
> Any help would be much appreciated!
>
> Warm regards,
> Fran
>
> --
> You received this message because you are subscribed to the Google Groups
> "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to
> psychopy-user...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/psychopy-users/-/zzGNSRsBuzkJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Fran

unread,
Aug 16, 2012, 1:37:00 PM8/16/12
to psychop...@googlegroups.com
Hello Jeremy,

thank you very much for your help, I think I'll get it to work now!

Take care,
Fran

Fran

unread,
Mar 16, 2013, 6:26:39 AM3/16/13
to psychop...@googlegroups.com
Hi Martin,

I did get this to work, however I'm still struggling with printing anything to the screen that's not English characters - i.e. I've just a hack to get it to print umlauts and such. But sure, here's the code if you just need it to do normal stuff. :)

       try:
            if 'space' in keys:
                filename='white.jpg'
                stim.setImage(filename)                   
                stim.draw()
                recog.draw()
                win.flip()
                sol=''
                while True:
                    if breaker:
                        break
                    for evkeys in event.getKeys():
                        if evkeys in ['escape']:
                            print evkeys
                            win.close()
                            core.quit()
                            event.clearEvents()
                        elif evkeys in ['backspace']:
                            sol=sol[:-1]
                            evmessage.setText(sol)
                            stim.draw()
                            recog.draw()
                            evmessage.draw()
                            win.flip()
                        elif evkeys in ['return']:
                            win.flip()
                            core.wait(1)
                            breaker = True
                            break
                        else:
                            sol+=str(evkeys)
                            evmessage.setText(sol)
                            stim.draw()
                            recog.draw()
                            evmessage.draw()
                            win.flip()
        except:
            pass

There's some bits of code in there that you won't need, but the basic idea is that once the user presses the space bar, they can enter their text. It then accepts a key press and adds it to a string. Once the user presses "Enter", the input is saved and they exit the loop.

I hope this helps? Give me a shout if you need more assistance, I've spent so much time trying to figure this out that I'm happy to pass on anything I've learned! :)

Warm regards,
Fran

On Thursday, March 14, 2013 12:26:21 PM UTC+1, Martin Petri Bagger wrote:
Hi Fran

Did you make it work, and if so would you be interested in sharing the code?
I am going to set up a choice experiment where the participants should be able to type in their choice. Unfortunately I am new to psychopy, but I have a bit of python programming experience. I am able to understand the code but not build code from scratch yet.
 
Best,
Martin

Martin Petri Bagger

unread,
Mar 18, 2013, 8:12:01 AM3/18/13
to psychop...@googlegroups.com
Hi Fran

Thank you very much

Best,
Martin

Martin Petri Bagger

unread,
Mar 18, 2013, 8:36:33 AM3/18/13
to psychop...@googlegroups.com
Hi again Fran

Would you mind sharing the entire experiment code? there is a lot of objects defined which I don't know where to define.

Best,
Martin

Fran

unread,
Mar 19, 2013, 12:14:25 PM3/19/13
to psychop...@googlegroups.com
Hi Martin,
 
I'd be reluctant to share the entire code, to be honest. (We haven't conducted the experiment yet.) I can probably give the relevant parts of the code.
 
Warm regards,
Fran

Paul Kieffaber

unread,
Jul 7, 2013, 10:47:50 PM7/7/13
to psychop...@googlegroups.com
So, it looks like this post has been going on for a while, but I thought I would share my simple solution.....
In short, I created two text displays (one for the question and the other for the response) and two keyboard components (one for collecting response and the other waiting for the 'return' key to end the routine) in a single routine.  Then a little code to convert the collected key presses to a string and replace key names with punctuation (e.g., 'space' = ' ').  Then overwrite the saved data at the end of the routine so that the CSV file contains a nice and neat set of text responses.

Don't know if anyone will find this useful.....but there it is.
Paul
TextToStreen.psyexp

Lily FitzGibbon

unread,
Aug 24, 2013, 2:15:39 AM8/24/13
to psychop...@googlegroups.com
Thank you. This code was very neat and very useful. I especially like having a separate keyboard component to wait for the 'return' key.

Lily

Zilong Xie

unread,
Dec 28, 2014, 2:02:29 PM12/28/14
to psychop...@googlegroups.com
Thank you for this template experiment. I noticed that in this template, there is not way  to correct my text input if I type something wrong.
 Do you know know how should I add this to your template?
Thanks!

best,
Zilong
Reply all
Reply to author
Forward
0 new messages