Stephen
YaBB Newbies
Offline
Posts: 31
Re: String and Numerical Input and Handling
Reply #5 - Oct 10th , 2008, 11:18pm
Hi Migs, I don't mean to answer for Fry, and of course I could not if I wanted to. I suspect you are seeing the usefulness (and possibly the general purpose) of Processing differently than its creators. General key and mouse input is extremely easy in Processing, there are built-in methods to handle it. You have a common but special case, the input of an entire line of text, and this is also easy, if you will allow me to demonstrate. The creators cannot (and should not in my opinion) attempt to foresee and implement every possible interaction the user might want, instead they give the building blocks with which to develop your own specialised interactions. The code below assumes you are familiar with the event model of programming. Bewarned, it does not use StringBuffer and it does not handle every key and there is only special processing for backspace and enter. Feel free to ask if you have any questions. Regards, Stephen String lastInput = new String(); String currentInput = new String(); void setup() { size(800, 600); smooth(); PFont font = loadFont("Arial-Black-48.vlw"); textFont(font); textAlign(CENTER); } void draw() { background(255, 255, 255); fill(0); text(lastInput, width/2, height/2); fill(255, 0, 0); text(currentInput, width/2, height*.75); } void keyPressed() { if(key == ENTER) { lastInput = currentInput = currentInput + key; currentInput = ""; } else if(key == BACKSPACE && currentInput.length() > 0) { currentInput = currentInput.substring(0, currentInput.length() - 1); } else { currentInput = currentInput + key; } }