Loading...
Logo
Processing Forum
In my applet, when users type using a coded key (for instance, if they press on the SHIFT key to write a capital letter or if they type on BACKSPACE to erase a character, there is a square character that is typed.

Does anyone know how this can be fixed?

To see what I mean, try typing a capital letter or backspacing in this applet:

http://www.learningprocessing.com/examples/chapter-18/example-18-1/

Replies(3)

Yes, check to see if key == CODED, and then ignore it, like so.

Copy code
  1. void keyPressed() {
  2.   // If the return key is pressed, save the String and clear it
  3.   if (key == '\n' ) {
  4.     saved = typing;
  5.     // A String can be cleared by setting it equal to ""
  6.     typing = ""; 
  7.   } else if (key == CODED) {
  8.     // ignore it.
  9.   } else {
  10.     // Otherwise, concatenate the String
  11.     // Each character typed by the user is added to the end of the String variable.
  12.     typing = typing + key; 
  13.   }
  14. }
Thanks jbum.

It works for most keys but not for the backspace key which is odd. The backspace key does not backspace and still generates a square character. This is my code (I added your keycode test):

void keyPressed() {
    fullComment = splitTokens(captureText, ", ");
    if (key == CODED) {
     // ignore it.
   }
   else {
    userInputText = userInputText + key;  // add each character typed by the user to the end of the userInputText String variable.
    // If the return key is pressed, save the String and clear it
    if (key == RETURN || key == ENTER) {
      captureText = userInputText;
      appendStrings("userinput.txt", fullComment);
      userInputText = "";  // Clear the String by setting it equal to ""
    }
   }
  }
}  // end of TextField class


Actually, I just discovered this hidden message at the bottom of that page (ie. http://www.learningprocessing.com/examples/chapter-18/example-18-1/#comments ) which gives, in a few lines of code, a beautiful solution that works like magic. It is signed Mr. Winkle.


For those wondering about deleting mistyped letters instead of

typing = typing + key;

use this:

if (keyCode == BACKSPACE) {
typing = typing.substring(0, typing.length() - 1);
}
else
if (key != CODED) typing += key;

greets, mr.winkle