Accents in Text Input

@LittleMary I had a similar problem. I need my participants to type in solutions to anagrams in Russian. The code I used for offline experiments worked fine (as you mention in the comment below, the older version of the keyboard allowed it). Unfortunately, I couldn’t make this code work online on Pavlovia.
I then used code from here and tried many options to make the use of Cyrillic letters possible, but only the following two worked for me.

For python
The solution is quite simple. I’ve just addressed to a different method.

# Begin routine
modify = False
text.text = ''
event.clearEvents('keyboard')
kb = keyboard.Keyboard() # here is something new

# Each frame
keys = kb.getKeys(waitRelease=False) # and here are some changes
if len(keys):
    if 'space' 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].name

# End routine
thisExp.addData("typedWord", text.text)

This code reads my keyboard configuration (Russian or English) and uses corresponding letters (Cyrillic or Latin).

For JavaScript
I didn’t manage to find a simple solution for JS to launch the experiment online. So, my variant here is pretty much the same as @Yeray_Mera suggested. I’ve just manually assigned particular Cyrillic symbol to each Latin symbol based on their layout.

// Begin routine and End routine are the same as in original code by @dvbridges
// Each frame
let theseKeys = psychoJS.eventManager.getKeys();

if (theseKeys.length > 0) { 
  textAdd = theseKeys[theseKeys.length-1]; 
  }

// To make it short, I put not the whole "alphabet", just an example
if (textAdd === 'q') {
    textAdd = 'й'
    text.text = text.text + textAdd;
} else if (textAdd === 'w') {
    textAdd = 'ц';
    text.text = text.text + textAdd;
} else if (textAdd === 'e') {
    textAdd = 'у';
    text.text = text.text + textAdd;
} else if (textAdd === 'r') {
    textAdd = 'к';
    text.text = text.text + textAdd;
} else if (textAdd === 't') {
    textAdd = 'е';
    text.text = text.text + textAdd;
} else if (textAdd === 'y') {
    textAdd = 'н';
    text.text = text.text + textAdd;
} else if (textAdd === 'u') {
    textAdd = 'г';
    text.text = text.text + textAdd;
} else if (textAdd === 'bracketleft') {
    textAdd = 'х';
    text.text = text.text + textAdd;
} else if (textAdd === 'bracketright') {
    textAdd = 'ъ';
    text.text = text.text + textAdd;
} else if (textAdd === 'semicolon') {
    textAdd = 'ж';
    text.text = text.text + textAdd;
} else if (textAdd === 'apostrophe') {
    textAdd = 'э';
    text.text = text.text + textAdd;
} else if (textAdd === 'comma') {
    textAdd = 'б';
    text.text = text.text + textAdd;
} else if (textAdd === 'period') {
    textAdd = 'ю';
    text.text = text.text + textAdd;
} else if (textAdd === 'quoteleft') {
    textAdd = 'ё';
    text.text = text.text + textAdd;
} else if (textAdd === 'return') {
    textAdd = ''; 
    continueRoutine = false;
} else if (textAdd === 'space') {
    textAdd = ' ';
} else if (textAdd === 'backspace') {
    text.text = text.text.slice(0, -1);
    textAdd = undefined;
} else {
    textAdd = undefined;
}

This solution is far from perfect, but it works. Hope, it’ll help you!

2 Likes