psychopy.org | Reference | Downloads | Github

Keyboard api with different language input

In MacOS, keyboard api ignores non-latin language input and just get the matching small alphabet letter.

from psychopy.core import getTime
from psychopy.hardware import keyboard
from psychopy import core

kb = keyboard.Keyboard()
stime = getTime()

# during your trial
while getTime()-stime < 1000.0:
    kb.clock.reset()  # when you want to start the timer from
    keys = kb.getKeys()
    if 'quit' in keys:
        core.quit()
    for key in keys:
        print(key.name, key.rt, key.duration)

In keyboard.py, I confirmed ‘key list’ as below.

keyNamesMac = {
    4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i',
    13: 'j', 14: 'k', 15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q',
    21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y',
    29: 'z',
    30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7',
    37: '8', 38: '9', 39: '0',
    40: 'return', 41: 'escape', 42: 'backspace', 43: 'escape', 44: 'space',
    45: 'minus', 46: 'equal',
    47: 'bracketleft', 48: 'bracketright', 49: 'backslash', 51: 'semicolon',
    52: 'apostrophe', 53: 'grave', 54: 'comma', 55: 'period', 56: 'slash',
    57: 'lshift',
    58: 'f1', 59: 'f2', 60: 'f3', 61: 'f4', 62: 'f5', 63: 'f6', 64: 'f7',
    65: 'f8', 66: 'f9', 67: 'f10', 68: 'f11', 69: 'f12',
    104: 'f13', 105: 'f14', 106: 'f15',
    107: 'f16', 108: 'f17', 109: 'f18', 110: 'f19',
    79: 'right', 80: 'left', 81: 'down', 82: 'up',
    224: 'lctrl', 225: 'lshift', 226: 'loption', 227: 'lcommand',
    100: 'function', 229: 'rshift', 230: 'roption', 231: 'rcommand',
    83: 'numlock', 103: 'num_equal', 84: 'num_divide', 85: 'num_multiply',
    86: 'num_subtract', 87: 'num_add', 88: 'num_enter', 99: 'num_decimal',
    98: 'num_0', 89: 'num_1', 90: 'num_2', 91: 'num_3', 92: 'num_4',
    93: 'num_5', 94: 'num_6', 95: 'num_7', 96: 'num_8', 97: 'num_9',
    74: 'home', 75: 'pageup', 76: 'delete', 77: 'end', 78: 'pagedown',
}

In contrast, the participant information input using pygui doesn’t show this kind of problem.
Is it possible to mod keyboard.py or pyglet to use MBCS(Multi Byte Character Set) or WBCS(Wide byte Character Set)?

I asked to Pygame github, and got a answer from ### illume that Pygame2 should be used.

You need pygame 2 (which is only available as a pre-release).

See examples/textinput.py
There isn’t really a tutorial yet (apart from that example). Hopefully you can figure it out from the example.

There are TEXTEDITING and TEXTINPUT events with these fields.

TEXTINPUT
    text

TEXTEDITING
    text
    start
    length

There is a TextInput API in SDL2 which talks about SDL_TEXTEDITING and SDL_TEXTEDITING events. IME.

Is there any plan to adopt Pygame2 in PsychoPy, or it will move on to another new backend?

These are quite different situations: when you are typing text into a text field, quite complicated things are being considered. Pushing the A key results in a letter a unless the shift key is held down, when you get a A. If you push either ctrl or command then no letter is typed at all, and a quite different event occurs.

PsychoPy, meanwhile, is operating at a very low level, being concerned with things like when a particular key is pushed, and also when it is released. We don’t wait for the sum of a number of keypresses to be interpreted by the operating system before determining what event occurred.

While this is important for things like reaction time studies, as you note, it does cause issues with just needing to handle formatted text input.

Pygame is deprecated in PsychoPy now, and with the new PsychoPy Keyboard class, we are moving away from Pyglet too, as the new class enables far superior timing abilities.

The good news is that this allows us to make changes to the code as necessary.

Looking at the code you found:

it doesn’t seem like the user can change the definition of those dictionaries at runtime. You could potentially monkey patch the keyboard.py module, to change the definition of keys to match what you want (you’d still need to handle upper case and so on manually).

But the ideal would be for someone to modify this code, either to allow the keyNames dictionary to be accessible to users so that can have individual entries altered, or to internally be more responsive to international keyboard settings.

Hi, thank you for the explanation.
In Psychtoolbox (with latest update), there are some functions to deal with the non-Latin-1 input issue as below.

Ask/GetString/GetEchoString/GetCharTest: Deal with Unicode key input. This now allows to take input in character sets / from keyboard layouts other than US layout with ASCII/Latin-1. E.g., german umlauts etc. should be processed properly. Only tested on Linux, but hopefully works on Windows/macOS as well.

I am not sure that the compiled binary from Psychtoolbox also contains GetString function.