We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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:
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).
key
can already be tested directly as a char. Conversion is unnecessary.See the example from the key reference:
To print it:
Or display it on screen:
Under most circumstances,
curr
is also unnecessary, becausekey
and keyCode are already built-in.@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 withkey
. Use it to type on the console line: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.
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...
@red_orchestra -- I think you are confused.
str(key)
is a String. If it was not a String then my demo sketch callingvoid 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 callingtext()
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, 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
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 toloadImage(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?
@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.
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!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???