From this Java sketch link below: http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR
void keyPressed() {
if (key != CODED | idx < 0) return;
final int k = keyCode;
final TextBox tbox = tboxes[idx];
final int len = tbox.txt.length();
if (k == LEFT) tbox.txt = tbox.txt.substring(0, max(0, len-1));
else if (k == RIGHT & len < tbox.lim-3) tbox.txt += " ";
}
Which in JS would be something like this:
function keyPressed() {
if (idx < 0) return;
const k = keyCode;
const tbox = tboxes[idx];
const len = tbox.txt.length;
if (k == LEFT) tbox.txt = tbox.txt.substring(0, max(0, len-1));
else if (k == RIGHT & len < tbox.lim-3) tbox.txt += " ";
}
yes, now with the if else structure
you not add the “Delete” key to the string ( before you do the delete ),
still what happens with all other keys what are not characters, like [SHIFT][x] ?
Thanks for the tips; I do plan to refactor later. I’m just in a mad dash to get the code running. I do like to reduce the number of lines… but I’ve found, in my limited javascript experience, that engines like extra brackets… not sure if it’s real or just perceived
GoToLoop, You’re right. It’s just perceived. Small correction though. The bob.length would need to be at the beginning.
else if (bob.length && keyCode == DELETE | keyCode == BACKSPACE )
bob = bob.substring(0, bob.length - 1);
But it turns out that I don’t even need to test for the length because substring will accept -1 in the second argument without causing any issues I’ve seen.