简体   繁体   中英

Standard in with Processing (processing.org)

This simple code has never worked for me in processing:

try {
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  String str = "";
  while (str != null) {
    System.out.print("> prompt ");
    str = in.readLine();
    println(str);
  }
} 
catch (IOException e) {
}

Probably because the console output box cannot be used for input, unlike in Eclipse. Is there a simple workaround, or am I forced to do something like a dialog box (or keyPressed handling) for standard in?

If you are using the Processing IDE, Processing does not support this behavior natively. If you export your sketch and edit the java files, or use Eclipse, Proclipsing, core.jar , etc. you can access the System.in like any other java application, however, this would defeat the purpose of processing in that it doesn't typically run from the command line and is graphic in nature.

Best practice would be to capture the keystokes with the key pressed method. For example:

String str = "";

void keyPressed() {
    str += key;
}

then in your draw() loop/method, you could handle the text input on str and clear it out if you wanted.

If you wanted something more sophisticated that would have a better UX, I suggest using something like ControlP5's TextField or TextArea.

Your program works perfectly (I named it test) and exported it as an Applet. I tested with cygwin as well as the windows command prompt:

$ cd test/applet
$ java -jar test.jar

Output (I typed "hello" and hit enter):

prompt> hello
hello
prompt>

I tried really quick on an Ubuntu terminal through ssh. I had issues getting it connected to the x11 server. Consider: http://en.wikipedia.org/wiki/Xvfb if that is an issue.

Just to confirm, I was able to run the SharedCanvasServer example included in Library->Network where I added a System.out.println to dump debug to the executing terminal.

java -cp "core.jar;net.jar;SharedCanvasServer.jar" SharedCanvasServer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM