psychopy.org | Reference | Downloads | Github

Getting typed response with non ASCII characters

OS (e.g. Win10): Mac OS Catalina 10.15.5
PsychoPy version (e.g. 1.84.x): 2020.1.2
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: Getting typed responses to pictures. In the task participants will complete a picture naming task. I’m using a custom code segment to display the written response on screen, so participants can see what they are typing. Since Swedish is the target language ä, ö, å should be included in the allowed keys. I (and the participants) are using Finnish/Swedish keyboards.

What did you try to make it work?: I’ve tried to map an alternative keyboard as instructed Accents in Text Input.

What specifically went wrong when you tried that?:
Nothing happens when ä/ö/å is clicked. The experiment does not crash from pressing the keys, but the following appears:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1121, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 158, in pygletKeyDown_
    symbol = getSymbol(nsevent)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 52, in getSymbol
    return charmap[chars[0].upper()]
KeyError: '\xc4'
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1121, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 164, in pygletKeyUp_
    symbol = getSymbol(nsevent)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 52, in getSymbol
    return charmap[chars[0].upper()]
KeyError: '\xc4'
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1121, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 158, in pygletKeyDown_
    symbol = getSymbol(nsevent)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 52, in getSymbol
    return charmap[chars[0].upper()]
KeyError: '\xc5'
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1121, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 164, in pygletKeyUp_
    symbol = getSymbol(nsevent)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 52, in getSymbol
    return charmap[chars[0].upper()]
KeyError: '\xc5'
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1121, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 158, in pygletKeyDown_
    symbol = getSymbol(nsevent)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 52, in getSymbol
    return charmap[chars[0].upper()]
KeyError: '\xd6'
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/libs/darwin/cocoapy/runtime.py", line 1121, in objc_method
    result = f(py_self, *args)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 164, in pygletKeyUp_
    symbol = getSymbol(nsevent)
  File "/Applications/PsychoPy3.app/Contents/Resources/lib/python3.6/pyglet/window/cocoa/pyglet_view.py", line 52, in getSymbol
    return charmap[chars[0].upper()]
KeyError: '\xd6'
##### Experiment ended. #####

I am completely new to PsychoPy, python and coding in general so any tips and tricks are much appreciated.

I also have one further question. As due to the pandemic, the experiment is going to be conducted through Pavlovia, so even if I manage to fix this, is the problem going to sustain once participants are completing the task on their own devices? (= is it worth fixing or should I come up with some alternative solution?)

Thanks!

Hi Rebecca,

The error you are seeing seems to be occurring in the pyglet library, which is a third party package that PsychoPy uses (i.e. it isn’t developed by us). Two things to consider:

  • pyglet is a Python package. It isn’t used when running online (as online experiments run in JavaScript rather than Python). So you should try your task online - it is possible that there is no issue there at all (or that another one arises…)
  • Partly because of issues like this, @jon has been developing a new text stimulus for PsychoPy that doesn’t rely on pyglet, and is completely under our control (it also allows for typed responses without the need for any custom code, both locally and in online experiments). I think this will be coming out out in the next release (hopefully later this month).

So all around, you should soon find an easier experience with showing typed responses shortly - although I don’t know what the accented character experience will be like.

Could you show us the code snippet you’re using? In particular, what code is extracting the text from the keyboard?

The next release will have an editable textbox and that should work with any keyboard/characters both online and locally

Hi!

Thanks for a quick reply! I’ll definitely try running the task online to see if the problem can be bypassed. I’ll post the results here once I get around to test that.

Great to hear that this is going to be made simpler in the next release. It’s much appreciated. :slight_smile:

Sure! This is what I’m using:

Begin routine:

textFill = ''

Each frame:

keys = event.getKeys()

if 'escape' in keys:
    core.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 key_resp.keys:
            textFill+=keys[0]  # Adds character to text 
copyText.setText(textFill)  # Set new text on screen

The original code that I’m using also had a import string + string.ascii_lowercase in the begin routine, but I have not included that. ‘ä’ ‘ö’ and ‘å’ are in the list of allowed keys in the keyboard component.

Thanks!

This code is checking the keyboard itself. So there shouldn’t be a graphical keyboard component at all - mixing the two approaches can easily conflict with each other.