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 & HelpPrograms › Reading input from keyboard
Page Index Toggle Pages: 1
Reading input from keyboard? (Read 510 times)
Reading input from keyboard?
Sep 21st, 2006, 9:04pm
 
Hi,

I am new to Processing and like it a lot so far. But I am puzzled: is there no way to read text input like System.in.read()?

TIA,

Michael.
Re: Reading input from keyboard?
Reply #1 - Sep 22nd, 2006, 11:41am
 
Processing reads keys individually, check out the reference for keyPressed(): http://processing.org/reference/keyPressed_.html
Re: Reading input from keyboard?
Reply #2 - Sep 22nd, 2006, 9:41pm
 
ok.

I tried

String input;
boolean done = false;
String result ="";


while (!done){

}
println( "\ninput: " + result);

void keyPressed(){
 if(key == CODED) {

   if ((keyCode == 16) || (keyCode == ENTER)) {
     //print( "DONE" + keyCode);      
     done = true;
   }

 }    
 else {
   print( key );
   result = result + key;
 }
 //println( "\n" + ergebnis );
}

but Processing doesn't like it -> it stalls. I also tried:


double value = 0.0;
 System.out.println( "start" );
try{
 byte [] b = new byte [200];
 System.in.read(b);
 value = (new Double(new String(b))).doubleValue();
 
 System.out.println( value );
}
catch(Exception e){
}
 System.out.println( "end" );


but that just outputs "start / end" and it's not waiting for input.

any help very much appreciated.

Cheerio,

Michael.
Re: Reading input from keyboard?
Reply #3 - Sep 22nd, 2006, 9:44pm
 
This example might help:

http://itp.nyu.edu/icm/shiffman/week9/typing/
Re: Reading input from keyboard?
Reply #4 - Sep 22nd, 2006, 9:53pm
 
Thanks for the applet! Actually, what I need is code that I can wrap into a method that returns a string/int, and which I can preferably call from the non-"setup+draw" mode.

e.g.
System.out.println("whats your size");
int s = getANumber();
if ( s > 12 ) ....

int getANumber(){
...
}

I.e. I do not want a method to be triggered when the int is entered, but rather have an algorithm that waits for input and continues when that is done. Maybe I am lacking some programming techniques here.

Thanks so far,

Michael
Page Index Toggle Pages: 1