psychopy.org | Reference | Downloads | Github

Writen response


#1

Hello!

I’m using PsychoPy 1.90.1 and windows 10.

I’m building a linguistic experiment and I’d like for the participants to rewrite a sentence they are presented with.
I started using the builder but then changed to the coder to try and modify the code. Adding some lines to the code for the keyboard response I’ve managed to capture all the keys the participant presses until ‘return’ is pressed. However, this is far from ideal… I can’t make the participant see what he’s writing which makes the task almost impossible, and I don’t really want ALL the keys he presses, just the final rewritten sentence.
Does somebody know a way to do this? Is there a pre-defined function that does this and I just haven’t been able to find it?

Thanks,
Alba


#2

Hi @alba, a simple solution involves using text and code components in Builder. Have one component display text called e.g., displayText, and another called e.g., copyText

In the code component, add the following code to the relevant tabs. Note, the shift keys to capitalize are not working at this point.

Begin Experiment

import string
allLetters = list(string.ascii_lowercase)

Begin Routine

textFill = ''

Each Frame

keys = event.getKeys()
if 'escape' in keys:
    core.quit()  # So you can quit
else:
    if keys:
        if keys[0] == 'space':
            textFill += ' '  # Adds a space instead of 'space'
        elif keys[0] == 'backspace':
            textFill = textFill[:-1]  # Deletes
        elif keys[0] in allLetters:
            textFill+=keys[0]  # Adds character to text if in alphabet.
        copyText.setText(textFill)  # Set new text on screen

#3

Thank you, @dvbridges!!
That solved my problem :slight_smile: And it was such a quick response, what a great surprise (it’s my first time in the forum and I thought it would take a while to get an answer)!

And since we are here…

I guess that if shift keys to capitalize are not working neither are special characters like ‘ç’ or ‘ã’, am I right? (I work with Portuguese and it has A LOT of those…)

In fact, I have another experiment in which the participants just assign a number to each sentence and in the data file the original sentence has lost its special characters and instead has some capital letters with other symbols, for example:
‘está’ becomes ‘está’

Do you know a way to solve this?

Thanks again!
Alba


#4

No problem @alba. If you do want capitalisation to work, you could use the following for “Every Frame”. The alternative is to use iohub, which has options of checking whether a particular key on the keyboard is down or up. That might make a more fitting solution, but for now:

keys = event.getKeys()

if 'escape' in keys:
    core.quit()
else:
    if keys:
        if keys[0] == 'space':
            textFill += ' '
        elif keys[0] == 'backspace':
            textFill = textFill [:-1]
        elif keys[0] in allLetters and not capitalize:
            textFill+=keys[0]
        # Dealing with shift
        elif keys[0] in ['lshift', 'rshift']:
            capitalize = True
            if len(keys)>1 and 'lshift' in keys or 'rshift' in keys:  # If shift and letter together
                keys = [key for key in keys if key not in ['lshift', 'rshift']]
        if capitalize == True and keys[0] in allLetters:
            textFill += keys[0].upper()
            capitalize = False
        text_2.setText(textFill)

As for the characters coming out incorrectly, it sounds like the character encoding is going wrong. How are you reading the data?


#5

Hi there @dvbridges!

Sorry for the delay, I’ve been crazy busy over here. I introduced a loop in the builder version which reads the sentences from a .xlsx file. Then the output file I get is a .csv which contains these weird transformations of special characters. I don’t know what I’m doing wrong :confused:

Thanks again for the code, that solves the capitalization perfectly! But my main concern is actually the special characters like ç ã á à that the subjects in the experiment may want to use… Is there a way to solve this? Maybe using their extended ASCII code?

Thank you so much for your help!
Alba


#6

It looks like PsychoPy is doing its job, and using several bytes to represent non-ASCII characters (using UTF-8 encoding). It is probably the case that the software you are using to open your data file is not Unicode aware, or at least, doesn’t default to assume that the text is UTF-8 encoded (let me guess, you’re using Excel, right?) If so, see the relevant part of this post:


#7

Thank you Michael, that solved it!
I feel a bit silly though, I should have looked for a topic on messy excel data… sorry!