psychopy.org | Reference | Downloads | Github

Typed response: punctuation

I have a coding for sentence production trial.
backspace, space, and return all work. But, I have no idea how to code for punctuations (e.g., period, comma).
Here is the coding I have:

if("backspace" in key_Resp.keys):
     key_Resp.keys.remove("bacspace")

elif("space" in key_Resp.keys):
     key_Resp.keys.remove("space")
     key_Resp.keys.append(' ')

elif("period" in key_Resp.keys):
     key_Resp.keys.append('.')

elif("return" in key_Resp.keys):
     key_Resp.keys.remove("return")

     screen_text = ' '.join(key_Resp.keys)
     thisExp.addData("recall_resp".screen-text)

     continueRoutine - False

screen_text = ' '.join(key_Resp.keys)

I recommend checking out this experiment for text input: https://gitlab.pavlovia.org/demos/textinput/

In the relevant section, you can add punctuation using the following:

if len(keys):
    if 'space' in keys:
        text.text = text.text + ' '
    elif 'period' in keys:
        text.text = text.text + '.'
    elif 'comma' in keys:
        text.text = text.text + ','
    elif 'apostrophe' in keys:
        text.text = text.text + '\''
    elif 'backspace' in keys:
        text.text = text.text[:-1]
    elif 'lshift' in keys or 'rshift' in keys:
        modify = True
    elif 'return' in keys:
        continueRoutine = False
    else:
        if modify:
            text.text = text.text + keys[0].upper()
            modify = False
        else:
            text.text = text.text + keys[0]

My colleague Emily Heffernan provided this workaround to me when I was having trouble with punctuation for text input.

2 Likes

Thank you so much! I SOLVED OUT!
I have one more question about capital letter “I”.
When participants type a sentence for each target word, they may need to use “I” instead of using “i”.
I added “I” in the keyboard (key_Resp), but it did not work.
I was not sure but I tried to code either:
elif(“caps lock” and “i” in key_Resp.keys):
key_Resp.keys.append(“I”)

or

elif(“lshift” and “i” in key_Resp.keys):
key_Resp.keys.append(“I”)

Sorry,
the problem solved.
I changed the code:
elif(“I” in key_Resp.keys):

  if(len(key_Resp.keys) < 2):
          key_Resp.keys.remove("i")
          key_Resp.keys.append("I")