Can't compare str(key) with an array

Hey, I'm working on my own multi-keystroke detection system but in order to clear out the cache I need to compare str(key) with an array list of items appended using the function str(key).

Unfortunately, I cannot figure out why it won't detect a match when comparing the two. Does anyone know how to solve this? To clarify, I am looking for a solution to my code or an explanation why it won't work if possible, thanks!

This is my first post with code, so let me know if I need to change anything, also I have a couple of other things in there from when was experimenting with different solutions, none worked and I'm out of options:

String[] keys = {};
String[] shortkeys = {};
int d = 0;

void setup(){
  frameRate(60);
}

void keyPressed(){
  keys = append(keys, str(key));
}

void keyReleased(){
  for(int i = 0; i < keys.length; i++){
    if(keys[i] == str(key)){
      keys[i].replace(keys[i], null);
    }
  }
}

void draw(){
  shortkeys = new String[0];
  if(keyPressed == false){
    keys = new String[0];
  }
  if(keys.length != 0){
  for(int i = 0; i < keys.length; i++){
  print(keys[i]);
  print("-");
  print(d);
  print("-");
  print(keys.length);
  print("-");
  }
  }
}
Tagged:

Answers

  • Thanks, used the code to fix my own and ended up with this, any feedback, warnings & other info would be appreciated!

    int kt = 0500;
    boolean[] keysDown = new boolean[kt];
    
    void setup(){
      frameRate(60);
    }
    
    void keyPressed(){
      keysDown[keyCode] = true;
    }
    
    void keyReleased(){
       keysDown[keyCode] = false;
    }
    
    void draw(){
      if(keyPressed){
        for(int i = 0; i < keysDown.length; i++){
          if(keysDown[i] == true){
           print(i);
          }
        }
      }
    }
    
  • edited June 14

    if (keysDown[i] == true) {

    For boolean types, there's no need to explicitly check for either true or false: O:-)

    Just this is enough: if (keysDown[i]) { :ar!

  • thanks, wasn't too sure about that one thanks for clearing it up :)

  • I noticed you are very good at compressing multiple parts/statements of code into one line & that's something I've always wanted to learn. Would you be able to give me any pointers to a tutorial that covers it well?

  • edited June 14

    I don't think there are any tutorials for it. Just dive deep into a language's syntax. ^#(^

  • edited June 15

    Will do. Ty for the help! :)

  • edited June 16

    Or better don’t.

    To compress code doesn’t give you more speed it just decreases readability and maintainability of the code.

    readability and maintainability is the main point of good code. When somebody else or you in a few months needs to read or change your code, he (or your future self) will be grateful for clear, longer and explicit code.

    Of course, I don't mean the == true example.

    To transform this if (keysDown[i] == true) { into if (keysDown[i]) { is fully ok.

    Chrisir ;-)

Sign In or Register to comment.