I am attempting to restrict the key presses to include only numbers and backspace for some textbox components on Pavlovia.
This issue was resolved for Python by @TParsons and @Michael here and it works great! However, it causes issues in Pavlovia (see screenshot) if I use the automatic Python to JS translator. I believe this is because of the structure of the join function (as indicated on @wakecarter 's cribsheet) but I am unsure how to alter it to make it work for Javascript.
Here is what works in Python:
weight1.text = "".join([ch.upper() for ch in weight1.text if ch.lower() in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace"]])
Here is the corresponding JS code that does not work:
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);
weight1.text = "".join(function () {
var _pj_a = [], _pj_b = weight1.text;
for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
var ch = _pj_b[_pj_c];
if (_pj.in_es6(ch.lower(), ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace"])) {
_pj_a.push(ch.upper());
}
}
return _pj_a;
}
.call(this));