psychopy.org | Reference | Downloads | Github

Get the unicode string input in Psychopy?

Displaying unicode text works quite well in PsychoPY3, via using u before unicode data.
However event.getKeys cannot receive any non-ascii input in MacOS. (I believe the situation might same in Windows).

It provides the error as below.

return charmap[chars[0].upper()]
KeyError:'\u3137'

Also we can test what_key.py demo in coder.
when I press ‘a’ key twice, the output shows it properly.

['a']
['a']

However, when I change the input keyboard through cmd+space and press same key, it shows below error.

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: '\u3141'

When use ‘input’ function in Python3.x commandline, it successfully store and print unicode keystrokes.
Is there any way to use input instead of PsychoPy key func. in PsychoPy?

OS (e.g. Win10): Mac OS 10.3.6
PsychoPy version (e.g. 1.84.x): 3
Standard Standalone? (y/n) If not then what?: y

You’re getting this the wrong way around. All strings are Unicode in Python 3. The u'' prefix is only necessary in Python 2.

It certainly can. As above, the string this function returns will be Unicode in Python 3.

Please just explain what you are trying to do.

The problem is not in PsychoPy or Python3. It seems the error happens in Pyglet level.
I tested several keyboard setting in MacOS High Sierra.
(English, Germany, Japanese, and Chinese)
When non-latin input is inserted to PsychoPy with Pyglet backend, it presents KeyError.
When change to the PyGame backend, it simply ignore keyboard language and receive alphabet.

Tried modified version (to work with Python3) of below,

https://github.com/jacanterbury/PsychoPy-snippets

and also tested the textStimuli_with_textResponse.py from below.
https://groups.google.com/forum/#!topic/psychopy-users/lE_bTMHUAoU

Yes, as you mentioned, there is no problem to present unicode text stimuli, or import unicode data from csv.
However, I am trying to get ‘text string (a.k.a. free text, not a single key)’ response of various non-latin languages and realized that none of existing codes works for it in PsychoPy3.

OK, so we’re talking international keyboard handling rather than Unicode strings per se. Yes, that can be a bit creaky.

Have you tried PsychoPy’s new Keyboard class?https://www.psychopy.org/api/hardware/keyboard.html

It supersedes the event module and provides much better timing. I’m not sure if it will handle the actual returned characters any differently, but at least it is more likely that we can get changes made to the source code if required.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

I checked new Keyboard class as you told.
It provides similar error when the key input is changed.
I think, the code modification is needed ‘international (non-ascii)’ text string input.