Need help with this code

I want the characters I type to be printed. When I press a letter key and SHIFT, I just want an upper letter to be printed.

The problem is, when I press a letter key and shift, the upper version of this letter is printed twice. Why is it printed twice? I just want it to be printed once.

And I don’t want to use void keyPressed().

This is my code:

void draw() {
  if (keyPressed) {
    if (keyCode == SHIFT) {
        keyPressed = false;      
    } else {
      print(key);
      keyPressed = false;
    }
  }
}

Thanks in advance :slight_smile:

Did you try

if (key != CODED)

See keyCode \ Language (API) \ Processing 3+

3 Likes

It would be better to use the getKeyCode() function within keyAdapter, check that code, and print the actual key. Otherwise, it would be better to use the key != CODED boolean within the processing development environment. Here is an implementation:

String code = "";
String[] words;
void draw() {
  words = code.split("/");
  println(words);
}
void keyPressed() {
  if (key != ' ') {
    if (keyCode == SHIFT) {
      String k = Character.toString(key);
      k = k.toUpperCase();
      code += k;
    } else {
      code += key;
    }
  } else {
    code += "/";
  }
}

3 Likes