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.
Page Index Toggle Pages: 1
Keyboard input (Read 585 times)
Keyboard input
Apr 26th, 2008, 5:31pm
 
How do I do this in Processing?:

import java.util.Scanner;
class Add1New
{
   public static void main(String[] args)
   {
     Scanner in =new Scanner(System.in);
     System.out.println("please enter a number");
     int n =in.nextInt();
     System.out.println("One more is " + (n+1));
   }
}
Re: Keyboard input
Reply #1 - Apr 26th, 2008, 8:46pm
 
processing is all about making visual things; keyboard input via the console isn't supported.
Re: Keyboard input
Reply #2 - Apr 30th, 2008, 1:01pm
 
I wonder whether this exclusive emphasis on the purely visual is something that the processing team and the user community is comfortable with. I find it somewhat confining. Why draw such a sharp boundary between the visual and the textual? I guess one can always say that those who are not happy with this should choose another platform, but Processing is a really helpful programming language for artists who would otherwise not consider programming. It is sad, in my view, to see people marking distinctions so sharply. (I live in China and love Chinese painting, where the writerly and the visual are not so sharply demarcated.).

I hope this posting does not sound confrontational. I really appreciate Processing, as I said, and feel thankful to those who developed this tool for free. I only mean to raise this question in response to the two postings in this thread.
Re: Keyboard input
Reply #3 - Apr 30th, 2008, 9:11pm
 
Actually it can be done in a crude sort of way. This is how my colleague Cristophe Rhodes at Goldsmiths did it:

StringBuffer buffer = new StringBuffer();
 boolean convert = false;
 
 void setup () {
   size(400,200); fill(0,0,0); smooth();
   PFont font = loadFont("CourierNew-12.vlw"); textFont(font);
 }
 

int readInt(int x,int y)
 {
   text(buffer.toString(), x, y);
   if(convert)
  return  Integer.parseInt(buffer.toString());
   else return 0;
 }
 
 void keyPressed() {
   convert = false;
   if (key >= '0' && key <= '9') {
     buffer.append(key);
   } else if (key == '-' && buffer.length() == 0) {
     buffer.append(key);
   } else if (key == BACKSPACE && buffer.length() > 0) {
     buffer.deleteCharAt(buffer.length() - 1);
   } else if (key == RETURN || key == ENTER) {
     if(buffer.length() > 0 || (buffer.length() > 1 && buffer.charAt(0) != '-')) {
       convert = true;
     }
   }
 }
 
 void draw () {
   background(200);
   text("please enter an int:", 40, 40);
   
  int x=readInt(60,60);    
  text("please enter an int:", 40, 60);
  convert=false;
  int y=readInt(60,80);
   if(convert) {
      text("the sum is ", 40, 90);
      text(x+y, 150, 90);
   }
 }
Re: Keyboard input
Reply #4 - May 1st, 2008, 12:12am
 
hector wrote on Apr 30th, 2008, 1:01pm:
I wonder whether this exclusive emphasis on the purely visual is something that the processing team and the user community is comfortable with. I find it somewhat confining. Why draw such a sharp boundary between the visual and the textual

It's not drawing a boundary to the textual. As the previous poster shows, you can use key input all you'd like. However *console* input is simply not supported and won't be any time soon.

Mac OS and Windows applications don't make any sort of use of a console, it exists only for technical types running command line applications. For 15 years before OS X, the Mac didn't even *have* a console. For a language that has been created for images, animation, etc, the console isn't even on the list as something that we should be going back to support.
Re: Keyboard input
Reply #5 - May 1st, 2008, 5:11am
 
A while back I needed a DOS-like console for something, so I put this together.  This isn't very full featured at all, as I ended up redoing everything for the project in Eclipse (and I don't have the .java files on me right now), but this could at least give you a start, and it's pretty simple:

Code:

PFont myFont;
String[] myString;
int numLines;
int lineSpacing = 14;
int counter;
int cursorBlinkDelay = 15;
int numColumns = 75;

void setup(){
size(640,480);
frameRate(30);
numLines = (int) (height/lineSpacing);
smooth();
myString = new String[numLines];
for (int i=0; i<numLines; i++){
myString[i] = "";
}
consolePrint("Welcome to the simple Processing console.");
consolePrint("You can't do much here, the only command that's implemented is");
consolePrint("printfile - just type 'printfile /filename.ext' to try it out.");
counter = 0;
myFont = createFont("Courier",48);
textFont(myFont,14);
}

void draw(){
background(0);
fill(255);

for (int i=0; i<numLines-1; i++){
text(myString[i],5,(i+1)*lineSpacing);
}
String addMe = "";
if (counter++ > cursorBlinkDelay) addMe = "_";
if (counter > 2*cursorBlinkDelay) counter = 0;
text(myString[numLines-1]+addMe,5,numLines*lineSpacing);
}

// This is the function you use to deal with the input
void processCommand(String s){
if (s.length()>9 && s.substring(0,9).toLowerCase().equals("printfile")){
printFile(s.substring(10));
}
else{
consolePrint("You said \""+s+"\"");
}
}

void consolePrint(String s){
myString[numLines-1] = s;
advanceLine();
}

void advanceLine(){
for (int i=0; i<numLines-1; i++){
myString[i] = myString[i+1];
}
myString[numLines-1] = ">>> ";
}

void keyPressed(){
if (key==ENTER||key==RETURN){
advanceLine();
String s = myString[numLines-2].substring(4);
processCommand(s);
return;
}
//Mac doesn't send BACKSPACE, it uses DELETE instead
if (key==BACKSPACE||key==DELETE){
if (myString[numLines-1].length()>4) myString[numLines-1] = myString[numLines-1].substring(0,myString[numLines-1].length()-1);
return;
}
println(keyCode);
if (key==CODED && keyCode==SHIFT) return;
else if (key==CODED) return;
myString[numLines-1] += key;
}

void printFile(String fileName){
String[] tryAdding = { "","/" };
for (int j=0; j<tryAdding.length; j++){
String tryFileName = tryAdding[j]+fileName;
try{
String[] myStrings = loadStrings(tryFileName);
for (int i=0; i<myStrings.length; i++){
consolePrint(myStrings[i]);
}
return;
}
catch(Exception e){
}
}
consolePrint("Sorry, \""+fileName+"\" was not found.");
return;
}
Page Index Toggle Pages: 1