Online text input

Description of the problem: I’m having issues converting my Python script to javascript for online text input. After searching and reading the forum, I’ve copied different code components and looked at the demos on Pavlovia but it still is not working for me. Specifically, I cannot figure out how to code in the backspace, shift and ? keys. I was able to do this fine in the builder. All the keys I need are working online except for shift, backspace and ?. I’ve pasted both codes below for reference.

Thank you all for any help.

My Python code is:
keys = event.getKeys()
if ‘escape’ in keys:
core.quit() #so you can quit the experiment

if len(keys):
if ‘space’ in keys:
text5.text = text5.text + ’ ’
elif ‘backspace’ in keys:
text5.text = text5.text[:-1]
elif ‘lshift’ in keys or ‘rshift’ in keys:
modify = True
elif ‘return’ in keys:
continueRoutine = False
elif ‘apostrophe’ in keys:
text5.text = text5.text + ‘’’
elif ‘period’ in keys:
text5.text = text5.text + ‘.’
elif ‘lshift’ and ‘slash’ in keys:
text5.text = text5.text + ‘?’
elif ‘right’ in keys:
text5.text = text5.text
else:
if modify:
text5.text = text5.text + keys [0].upper()
modify = False
else:
text5.text = text5.text + keys[0]

and my Javascript code is:
let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) { // at least one key was pressed
textAdd = theseKeys[theseKeys.length-1];
}

// check for quit:
if (theseKeys.indexOf(‘escape’) > -1) {
psychoJS.experiment.experimentEnded = true;
}
if (textAdd) {
if (theseKeys.indexOf(‘space’) > -1) {
text5.setText(text5.text + " ");
textAdd = undefined; // Add a space
} else if (theseKeys.indexOf(‘lshift’) > -1) {
text5.setText(text5.text + “”);
textAdd = undefined;
} else if (theseKeys.indexOf(‘apostrophe’) > -1) {
text5.setText(text5.text + “’”);
textAdd = undefined;
} else if (theseKeys.indexOf(‘backspace’) > -1) {
text5.setText(text5.text + textAdd[textAdd.length-1]);
textAdd = undefined;
} else if (theseKeys.indexOf(‘period’) > -1) {
text5.setText(text5.text + “.”);
textAdd = undefined;
} else if (theseKeys.indexOf(‘right’) > -1) {
text5.setText(text5.text + “”);
textAdd = undefined;
} else if (theseKeys.indexOf(‘slash’ & ‘lshift’) > -1) {
text5.setText(text5.text + “?”);
textAdd = undefined;
} else {
text5.setText(text5.text + textAdd);
textAdd = undefined;
}
}

I suspect it’s a key-code mismatch issue, something having a different name in JS than Python. In the first part of your JavaScript, try this:

let theseKeys = psychoJS.eventManager.getKeys();
if (theseKeys.length > 0) { // at least one key was pressed
    textAdd = theseKeys[theseKeys.length-1];
    console.log(textAdd);
}

That will give you a direct list of the key codes that are in textAdd in your browser’s JavaScript console (in chrome, you can find this under view -> developer), which you can then use to determine how JavaScript is capturing “?”, shift, and backspace.

1 Like