How can i erase character from a text string by pressing the back space key.
This is what i have got so far. It only deletes the last character.
if (keyPressed) {
if (key != '\n' && key != CODED) {
if (typing.length() < 5){
typing = typing + key;
}
}
if (key == BACKSPACE) {
if (typing.length() > 0) {
typing = typing.substring(0, typing.length()-1);
}
}
}
text(typing, 345, 372);
That’s because
BACKSPACEis notCODED, so whiletyping.length()is smaller than5and you pressBACKSPACEyou meet both conditions breaking stuff. When it reaches5it only mets second condition:if (key == BACKSPACE)so it works, bringing it back to less than5so it won’t work again…As a test try:
if (key != '\n' && key != CODED && key != BACKSPACE)But i think you might want a
switch (key)to do the job.Also you better use
void keyPressed()orkeyReleased()orkeyTyped()Instead of
keyPressedthe field, in draw(). Or you will need to handle key repetition yourself…