We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › key value on KeyPressed >> to string
Page Index Toggle Pages: 1
key value on KeyPressed >> to string ? (Read 489 times)
key value on KeyPressed >> to string ?
Nov 3rd, 2008, 8:13pm
 
Hello everyone !
So, I need for a particular reason to convert the key value returned by the keyPressed() function to a string. Here is the code ;

import ddf.minim.*;

Minim _minim;
HashMap _soundBox;

void setup(){
 size(100, 100);

 _minim = new Minim(this);
 _soundBox = new HashMap();

 //get the data folder on my sketch
 java.io.File folder = new java.io.File(dataPath(""));
 java.io.FilenameFilter wavFilter = new java.io.FilenameFilter() {
   boolean accept(File dir, String name) {

     //returned all wav files
     return name.endsWith(".wav");  
   }
 };

 //populate an array for the returned values
 String[] filenames = folder.list(wavFilter);

 //populate an Hashmap with the first letter of the files for key and the filename for value
 for (int i = 0; i < filenames.length; i++) {
   _soundBox.put(filenames[i].substring(0,1), filenames[i]);
 }

 //unmark the next block if you need to understand
 /*
 Iterator i = _soundBox.entrySet().iterator();
  while (i.hasNext()) {
  Map.Entry me = (Map.Entry)i.next();
  println(me.getKey() + " is ");
  println(me.getValue());
  }
  */
}

void draw(){
 background(0);
}

//THE PROBLEM
void keyPressed(){
 //this method returns an error "Cannot cast from char to String"
 println((String)key);
 
 //this one too... "Cannot invoke tostring() on the primitive type char"
 println(key.tostring());
 
 //return null
 println("#Hashmap for " + key + "# >> "+_soundBox.get(key));
 
 //return the proper value assuming you have a wav file that begins with "q"
 println("#Hashmap for q# >>"+_soundBox.get("q"));
}

If I need this, it's because each one of my "wav files" begin with a specific letter that I want to link with the keyressed() function. So, if I would like to link more files with a keyPressed event, I just would have to rename the file with the key I wish ("gpianosample.wav", for example), without touching anything inside the program.

Hope that first post will be answered. (hope you understand my english too...)
Re: key value on KeyPressed >> to string ?
Reply #1 - Nov 4th, 2008, 7:50am
 
You can convert a char to String with:

String sk = new String(key);

There might be better, simpler solutions, or not, not much time to investigate right now.
Re: key value on KeyPressed >> to string ?
Reply #2 - Nov 4th, 2008, 10:04am
 
either use:
http://processing.org/reference/str_.html

or just:
println( key + "" );  // will be cast to String automatically

F
Re: key value on KeyPressed >> to string ?
Reply #3 - Nov 4th, 2008, 12:05pm
 
HIGH !
Thanks a lot. I resolved it.
Page Index Toggle Pages: 1