OS (e.g. Win10): Win11
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2025.1.1
Standard Standalone Installation? (y/n) If not then what?: Y
Do you want it to also run locally? (y/n) N
What are you trying to achieve?:
I am running a judgement task online and I would like to restrict inputs on the inputs to numerical values only (e.g., (0-9)). For a keyboard component called test_resp and textbox called TestingResponse, I used some code I found on the forum (link below) in the ‘each frame’ part of a code component in the Builder.
keys = test_resp.getKeys()
correctKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
if keys:
if keys[-1] not in correctKeys:
TestingResponse.text = TestingResponse.text[0:-1]
What specifically went wrong when you tried that?:
This worked fine locally (when I type a non-numeric input in TestingResponse, it deletes itself), but when I tried to run this online via both Pavlovia and the piloting in JS option, I am not able to input anything at all keyboard wise. I wonder if it may be something to do with the auto translation to PsychoJS (translated code below)?
To confirm, this doesn’t affect other textboxes where this code snippet isn’t applied (e.g., I have a condition number field at the start that I use for testing).
What did you try to make it work?: I tried checking if there was some setting on my laptop that didn’t allow keyboard responses to be tracked but this wasn’t the case. I also checked some obvious things like the textbox input being in a visible colour (it is).
Link to the most relevant existing thread you have found: Problem restricting textbox input to numbers only for TParsons original code I went from
Any assistance would be greatly appreciated. Thank you!
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 = test_resp.getKeys();
correctKeys = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
if (keys) {
if ((! _pj.in_es6(keys.slice((- 1))[0], correctKeys))) {
TestingResponse.text = TestingResponse.text.slice(0, (- 1));
}
}