There’s a bunch of stuff going on here and it’s going to take some work. What you’ve posted here is Python code, and some of it we can change in the Python, but some of it is going to require making modifications to the JS directly after it’s been converted.
Step 1: Let’s modify the Python. For this part, keep the “code type” of your code component as “auto->JS”.
-
core.quit() isn’t needed, and PsychoPy has other ways of detecting if the “escape” key has been pressed, so we don’t need that top-level “if” statement at all.
-
That whole try/catch thing isn’t needed. shift_flag is declared much earlier anyways. In fact, you can just pick up from keys=event.getKeys()
Here’s what the Python code will look like:
Python code component (collapsed for space)
keys = event.getKeys()
if keys:
if keys[0] == 'space':
textFill += ' '
elif keys[0] == 'backspace':
textFill = textFill[:-1] # Deletes
elif keys[0] == 'return':
textFill += '\n'
elif keys[0] == 'right':
# record the answer in your data file:
thisExp.addData('answer', textFill)
# go on to the next trial:
continueRoutine = False
elif keys[0] in ['lshift', 'rshift']:
shift_flag=True
pass
elif keys[0] == 'period':
textFill = textFill+'.'
elif keys[0] == 'plus':
textFill = textFill+'+'
elif keys[0] == 'minus':
textFill = textFill+'-'
elif keys[0] == 'comma':
textFill = textFill+','
elif keys[0] == 'capslock':
shift_flag == True
elif shift_flag == True and keys[0] == '0':
textFill = textFill+'='
elif shift_flag == True and keys[0] == '8':
textFill = textFill+'('
elif shift_flag == True and keys[0] == '9':
textFill = textFill+')'
elif shift_flag == True and keys[0] == 'ß':
textFill = textFill+'?'
elif shift_flag == True:
textFill = textFill+keys[0].upper()
shift_flag = False
else:
textFill = textFill+keys[0]
screenText.setText(textFill) # Set new text on screen
Once you’ve got that code in the left panel of your code component, change the code type from “Auto->JS” to “Both”. That will retain the auto-converted code, but you can now modify the JS directly in the right panel (this is so we don’t break the python code while we’re changing the JS). Everything else we do in the right-hand panel of the code component. It should look like this at first:
Initial JS code (collapsed for space)
var _pj;
function _pj_snippets(container) {
function in_es6(left, right) {
if (((right instanceof Array) || ((typeof right) === “string”))) {
return (right.indexOf(left) > (- 1));
} else {
if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
return right.has(left);
} else {
return (left in right);
}
}
}
container[“in_es6”] = in_es6;
return container;
}
_pj = {};
_pj_snippets(_pj);
keys = event.getKeys();
if (keys) {
if ((keys[0] === “space”)) {
textFill += " ";
} else {
if ((keys[0] === “backspace”)) {
textFill = textFill.slice(0, (- 1));
} else {
if ((keys[0] === “return”)) {
textFill += “\n”;
} else {
if ((keys[0] === “right”)) {
thisExp.addData(“answer”, textFill);
continueRoutine = false;
} else {
if (_pj.in_es6(keys[0], [“lshift”, “rshift”])) {
shift_flag = true;
} else {
if ((keys[0] === “period”)) {
textFill = (textFill + “.”);
} else {
if ((keys[0] === “plus”)) {
textFill = (textFill + “+”);
} else {
if ((keys[0] === “minus”)) {
textFill = (textFill + “-”);
} else {
if ((keys[0] === “comma”)) {
textFill = (textFill + “,”);
} else {
if ((keys[0] === “capslock”)) {
(shift_flag === true);
} else {
if (((shift_flag === true) && (keys[0] === “0”))) {
textFill = (textFill + “=”);
} else {
if (((shift_flag === true) && (keys[0] === “8”))) {
textFill = (textFill + “(”);
} else {
if (((shift_flag === true) && (keys[0] === “9”))) {
textFill = (textFill + “)”);
} else {
if (((shift_flag === true) && (keys[0] === “\u00df”))) {
textFill = (textFill + “?”);
} else {
if ((shift_flag === true)) {
textFill = (textFill + keys[0].upper());
shift_flag = false;
} else {
textFill = (textFill + keys[0]);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
screenText.setText(textFill);
First thing we’ll need to change is the line keys=event.getKeys();
because that won’t work in JS. Instead, we change it to keys = psychoJS.eventManager.getKeys();
Next, if (keys){
probably won’t work as well as if (keys && keys.length){
. This is just a belt-and-suspenders thing to make sure there’s actually something in the keys array. The underlying reasons to do this in JS but not Python involve how “if” statements work, a concept called “truthyness”, and a whole bunch of other stuff we don’t need to get into.
Then we need to replace thisExp.addData
. PsychoJS just uses addData in a different way. Use psychoJS.experiment.addData
instead.
Finally, keys[0].upper()
won’t work, because .upper() is another Python function. There is a JS equivalent, however: keys[0].toUpperCase()
Ultimately, that will leave you with this JS code:
final JS code
var _pj;
function _pj_snippets(container) {
function in_es6(left, right) {
if (((right instanceof Array) || ((typeof right) === "string"))) {
return (right.indexOf(left) > (- 1));
} else {
if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
return right.has(left);
} else {
return (left in right);
}
}
}
container["in_es6"] = in_es6;
return container;
}
_pj = {};
_pj_snippets(_pj);
keys = psychoJS.eventManager.getKeys();
if (keys.length) {
if ((keys[0] === "space")) {
textFill += " ";
} else {
if ((keys[0] === "backspace")) {
textFill = textFill.slice(0, (- 1));
} else {
if ((keys[0] === "return")) {
textFill += "\n";
} else {
if ((keys[0] === "right")) {
psychoJS.experiment.addData("answer", textFill);
continueRoutine = false;
} else {
if (_pj.in_es6(keys[0], ["lshift", "rshift"])) {
shift_flag = true;
} else {
if ((keys[0] === "period")) {
textFill = (textFill + ".");
} else {
if ((keys[0] === "plus")) {
textFill = (textFill + "+");
} else {
if ((keys[0] === "minus")) {
textFill = (textFill + "-");
} else {
if ((keys[0] === "comma")) {
textFill = (textFill + ",");
} else {
if ((keys[0] === "capslock")) {
(shift_flag === true);
} else {
if (((shift_flag === true) && (keys[0] === "0"))) {
textFill = (textFill + "=");
} else {
if (((shift_flag === true) && (keys[0] === "8"))) {
textFill = (textFill + "(");
} else {
if (((shift_flag === true) && (keys[0] === "9"))) {
textFill = (textFill + ")");
} else {
if (((shift_flag === true) && (keys[0] === "\u00df"))) {
textFill = (textFill + "?");
} else {
if ((shift_flag === true)) {
textFill = (textFill + keys[0].upper());
shift_flag = false;
} else {
textFill = (textFill + keys[0]);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
screenText.setText(textFill);
You can actually copy that into the JS panel of your code component if the code type is set to “both”.
Looking at it there are a couple things in it that I’m not 100% sure will work, but I don’t have time to test it myself right now. I would recommend making these changes and piloting it on Pavlovia, and if it gives you any errors they will probably be different from what you were getting before, and informative as to what else might need to change.