Convert key to str for use in function

Hi, I'm having a little trouble resolving a function. The idea is to make every key on the keyboard do something different. But for some reason I can't resolve the char of "key" into a string - when I try the following basic code, nothing happens when I hit a key

It goes something like this (I will just paste the relevant code):

    void setup(){
      size(300, 300);
      background(255, 0,0);
    }

    void draw(){

    String curr = "0";
      curr = str(key); //parenthesis (pause!)

      if(curr == "p"){
        background(0);
      }
      if(curr == "q"){
        background(255);
      }

      println(curr + " is the current key");
    }

println shows me that the key is working in conversion, but for some reason I can't seem to get the function working. I've trawled the forums but haven't come across this - I would be very grateful if anyone can point me to an answer in another forum post, or let me know if you can spot what I'm doing wrong!

Thanks!

Answers

  • println is doing an implicit conversion iirc

    you can test key like key == 'q'

    because it's a char and this ' ' is for char

    you can't check a String with == though, better:

    if ("hello".equals("hello"))
      println ("Yes");
    
  • Thanks - I understand the ' ' for char and the " " for string, I think, but what I am struggling with is how to resolve str(key) so the char is converted to String. The println was just for testing (but thanks for solving that, so I at least understand why my test is failing).

  • edited December 2016

    key can already be tested directly as a char. Conversion is unnecessary.

    See the example from the key reference:

    void draw() {
      if (keyPressed) {
        if (key == 'b' || key == 'B') {
          fill(0);
        }
      } else {
        fill(255);
      }
      rect(25, 25, 50, 50);
    }
    

    To print it:

    println("My key: " + key);
    

    Or display it on screen:

    text( "My key: " + key, 10, 10);
    

    Under most circumstances, curr is also unnecessary, because key and keyCode are already built-in.

  • edited December 2016

    @red_orchestra --

    Also, just to demonstrate that your use of str(key) is correct, here is a simple (although pointless) test sketch that calls a String function with key. Use it to type on the console line:

    void draw() {
    }
    void keyTyped(){
      printString(str(key));
    }    
    void printString (String s){
      print(s);
    }
    

    The point is, however, that the char version is much easier to test and print, and the built-in variable is much easier than trying to manage a parallel variable. Unless you have to convert key to a string for a specific purpose, don't bother.

  • color bg;
    
    void draw() {
      background(bg);
    }
    
    void keyPressed() {
      final int k = keyCode;
      if (k == 'P' | k == ' ')  bg ^= -1;
    }
    
  • Thanks all. I'm afraid the solutions are adequate to work around the problem, but not really to make the conversion work.

    @jeremydouglass: I thought the same re 'print' proving the conversion, but my next step was to use the string to loadImage(char + ".jpg") and then draw image(char), in order to make it tidy I needed the string conversion. This fails miserably, which leads me to believe that @Chrisir was correct in saying that the conversion is automatically done. I worked backwards into a simple program (the one above) to see if I could simply change the background colours but with no luck, which is why I asked for help :/ - sorry for any confusion

    I've managed to cludge this together with two arrays (one char[], one String[]) and matching the array points, but it's an untidy solution to a problem that seems simple but is doing my head in...

  • edited December 2016
    void draw() {
      final int k = keyCode;
      final color bg = -int(k == 'P' | k == ' ');
      background(bg);
    }
    
  • @red_orchestra -- I think you are confused. str(key) is a String. If it was not a String then my demo sketch calling void printString (String s) would throw an error. There is no conversion that is failing, or being automatically done, in my demo sketch -- regardless of println() being used. Try calling text() instead -- it will work the same.

    I'm not sure what your loadImage problem is -- you haven't shared that code -- but it isn't that!

  • Maybe this will help you understand why you can't check Strings with "==" -
    http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

  • Don't check key against a string.

    Do not convert it at all.

    It is a character.

    Compare it to a character.

    if( key == 'j'){
    

    If, for some crazy reason, you do need to convert it to a string (and again, you should not need to), then you have to compare using .equals

    if( str(key).equals("j") ){
    
  • Thanks everyone. As I mentioned, there is a reason that I need to convert to str, unless I can load images through: loadImage(char + ".jpg"); which won't work, so I want to loadImage(str(char) + ".jpg");. The advantage to using a single array with only str is for more efficient code.

    The suggestion above: if(str(key).equals("j")) can't manage what I'm hoping for either. I have an alternative, as posted, using two arrays, but I'm still curious as to why this doesn't work...

  • Okay, now I have to ask why you are loading files somewhere other than setup(). What gives?

  • edited December 2016

    @red_orchestra -- re: "can't manage what I'm hoping for."

    This is getting a bit silly. Your question has been answered several times now. If you have broken code that you cannot fix, please just share it!

    The example below tests if key equals the letter "p". It then uses the value as part of a string to load an image file. Why you would want to do this is another question, but this works exactly as shown in the examples.

    // press p to Processing
    // 2016-12-14
    void setup(){ size(512,512); }
    void draw() { }
    void keyTyped(){
      if(key=='p'){
        PImage img = loadImage("https:" + "//processing.org/img/" + key + "rocessing3-logo.png");
        image(img,0,0);
      }
    }
    

    If you want to, you can also copy key into another, separate char before doing the test or doing the file loading -- although there is no reason to do so!

      char curr = key;
      if(curr=='p'){
        PImage img = loadImage("https:" + "//processing.org/img/" + curr + "rocessing3-logo.png");
        image(img,0,0);
      }
    

    As mentioned before: I'm not sure what your loadImage problem is -- you haven't shared that code -- but it isn't that!

  • You haven't really asked the question you needed to ask. What exactly is your question???

Sign In or Register to comment.