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 › Getting numbers from the keyboard
Page Index Toggle Pages: 1
Getting numbers from the keyboard (Read 408 times)
Getting numbers from the keyboard
Nov 28th, 2007, 3:53am
 
I am quite new to processing and have come across some problems when trying to get and store numbers from the keyboard.

For example I am asking the user for his/her age and the user types 65 or 102 then how can I get processing to read 65 and store it in a variable?
Re: Getting numbers from the keyboard
Reply #1 - Nov 28th, 2007, 8:50am
 
hi,
this might help. a number is confirmed by the user with the return key.
Code:

String myNumberContainer;
int yourAge;
int ageLimit;

void setup() {
size(400,400);
myNumberContainer = "";
yourAge = 0;
ageLimit = 120;
}

void draw() {}

void keyPressed() {
// check if a key between 0 and 9 has been pressed
if(keyCode>=48 && keyCode<=57) {
myNumberContainer += key;
}
// if the user presses return, convert the
// numbers collected in myNumberContainer to an int
if(keyCode==10) { // 10 = return
yourAge = int(myNumberContainer);
if(yourAge>ageLimit) {
yourAge = ageLimit;
}
println("your age: "+yourAge+" / age typed in: "+myNumberContainer);
myNumberContainer = "";
}
// to delete the last digit typed, in use the following block
if(keyCode==8) { // 8 = delete/backspace
if(myNumberContainer.length()>0) {
myNumberContainer = myNumberContainer.substring(0,myNumberContainer.length()-1);
}
else {
myNumberContainer = "";
}
}
}

Re: Getting numbers from the keyboard
Reply #2 - Nov 28th, 2007, 9:08am
 
You could also use the GUI libraries to display a textfield so that the user can type his age in the box. You can then detect when the "ENTER" key is hit or you could also add a "Submit" button.

Since you're quite new, I suggest you take a look at this library. The example are very easy to follow!

http://www.repeatwhiletrue.com/SpringGUI/

Cheers!
Re: Getting numbers from the keyboard
Reply #3 - Nov 28th, 2007, 5:25pm
 
Cheers guys you have really helped me out here.
Page Index Toggle Pages: 1