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 › keyboard input, trying to input a two digit number
Page Index Toggle Pages: 1
keyboard input, trying to input a two digit number (Read 2737 times)
keyboard input, trying to input a two digit number
Feb 2nd, 2010, 1:23pm
 
hey there,

i've been trying to control the radius of a circle by entering a two digit number ( e.g 12, 05, 89) for the radius. I've been looking at key, keyPressed, key events etc, but have only found useful info for entering a one digit number.

to get a two digit number, i've been thinking along the lines of...

int num = int(key);
num = num - 48;    //int(0) = 48
num = num*10;     //For the 10s value
int num2 = int(key);    
num2 = num2 - 48;
int radius = num + num2;

but as a result the key value stays the same, so i radius values of 11, 22, 33, 44, 55, 66, 77, 88, 99. Is there a way of clearing the key variable and re-entering data, or a away of entering a two-digit number that i cant see, or something else??

Thanks for your help, its not a very technical question but it seems to be hindering me a bit.

Matt
Re: keyboard input, trying to input a two digit number
Reply #1 - Feb 2nd, 2010, 3:47pm
 
searching on the forums, i have found one post which has been useful, which is good, I've managed to get it to work, but its a bit messy, for example you can't see what you are typing. anyone know of a cleaner way?

int getInt(){       //function to return radius value
 if(convert)
 return Integer.parseInt(intInput.toString());
 else return 0;
}

void keyPressed() {
  convert = false;
  if (key >= '0' && key <= '9') {
    intInput.append(key);
  } else if (key == '-' && intInput.length() == 0) {
    intInput.append(key);
  } else if (key == BACKSPACE && intInput.length() > 0) {
    intInput.deleteCharAt(intInput.length() - 1);
    println("Current Radius: "+intInput.toString());
  } else if (key == RETURN || key == ENTER) {
    if(intInput.length() > 0 || (intInput.length() > 1 && intInput.charAt(0) != '-')) {
      convert = true;
    }
  }
}
Re: keyboard input, trying to input a two digit number
Reply #2 - Feb 2nd, 2010, 5:50pm
 
Here's how I'd do it:

Quote:
String input = "";
void setup(){
}
void draw(){
}
void keyPressed(){
  if( key >= '0' && key <= '9' ){
    input+=char(key);
    println( "The key entered was: " + char(key) );
    println( "The current input is: " + input );
    if( abs( int( input ) ) > 100 ){
      println( "Whoops! That's too many digits. Let's ignore that last one." );
      input = input.substring(  0, input.length()-1 );
      println( "The current input is: " + input );    
    }
  } 
  else if( key == '-' && input.equals( "" ) ){
    input+='-';
    println( "The key entered was: " + char(key) );
    println( "The current input is: " + input );
  } 
  else if( key == BACKSPACE && input.length() > 0 ){
    println( "Didn't like that last key you pressed, huh? Ok, we'll get rid of it for you." );
    input = input.substring(  0, input.length()-1 );
    println( "The current input is: " + input );
  } 
  else if (key == RETURN || key == ENTER){
    int finalInput = int( input );
    println( "Input entered is: " + finalInput );
    input = "";
  }
}

Re: keyboard input, trying to input a two digit number
Reply #3 - Feb 3rd, 2010, 3:23am
 
Hey thats really helpful, thanks alot, much cleaner.
Re: keyboard input, trying to input a two digit number
Reply #4 - Feb 3rd, 2010, 4:24am
 
Of course, it has a bug in it.  Roll Eyes Looking at it again I see that

if( abs( int( input ) ) > 100 ){

should really be

if( abs( int( input ) ) >= 100 ){

My bad.  Embarrassed
Re: keyboard input, trying to input a two digit number
Reply #5 - Feb 3rd, 2010, 6:20am
 
yep i just changed it to 99 lol, it seems to also crash if for example, 6 was just entered, rather than 06, so it HAS to be two-digits, but other than that it works a treat Smiley
Two digit keyboard input, similar to Photoshop
Reply #6 - Feb 3rd, 2010, 9:34am
 
Try this on for size... similar to Photoshop controls (for layer opacity and other things requiring a % value).

"1" -> 10
"2" -> 20
"9" -> 90
"0" -> 100

"01" -> 1
"10" -> 10
"23" -> 23
"99" -> 99
"00" -> 100

A delay of more than 400ms (see variable entrySeparation) between keystrokes allows you to press "1" to mean "10" followed shortly after (but not too shortly) by "2" to mean "20".

-spxl

Code:
/**
* spxlTwoDigitInput
* 2010-02-04 by subpixel
* http://subpixels.com
*
* Use keyboard input to control a numeric parameter
* Single digit entry gets "simple" value (eg 10, 20, 30, ...)
* Two digit entry gets "refined" value (eg 03, 25, 99)
* '0' and '00' are treated as 100
*/

String inputStr = "";
int inputVal = 100;
long lastDigitTime = 0;

// Sequence of digits allowed with up to
// this many milliseconds between keypresses
long entrySeparation = 400;

int fontSize = 20;
int tx, ty; // Text location used in txt()

void setup()
{
 size(640, 480);
 textFont(createFont("", fontSize)); // default font
 ellipseMode(RADIUS);
 noCursor();
}

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

 tx = fontSize;
 ty = fontSize;
 
 txt("Use digits to set radius:");
 txt("- Single digit sets 10, 20, 30, ... 100");
 txt("- Two digits sets 01, 02, 03, ... 99");
 txt("");
 txt("UP and DOWN adjust radius");
 txt("");
 txt("inputStr: [" + inputStr + "]");
 txt("inputVal:  " + inputVal);
 
 translate(mouseX, mouseY);

 stroke(255, 0, 0);
 
 line(-110, 0, 110, 0);
 line(0, -110, 0, 110);
 
 int tickWidth = 2;
 for (int tick = -100; tick <=100; tick += 10)
 {
   if (tick == 0) continue;
   
   line(-tickWidth, tick, tickWidth, tick); // y-axis ticks
   line(tick, -tickWidth, tick, tickWidth); // x-axis ticks
 }
 

 stroke(255);
 noFill();
 ellipse(0, 0, inputVal, inputVal);
}

// Utility on-screen text printing method
void txt(String s)
{
 text(s, tx, ty);
 ty += fontSize;
}

void keyPressed()
{
 long time = millis();
 println("keyPressed(): key: [" + key + "]");
 
 if (key == CODED)
 {
   switch(keyCode)
   {
     case UP:   inputVal += 1; break;
     case DOWN: inputVal -= 1; break;
   }
   inputVal = constrain(inputVal, 01, 100);
 }
 else if (Character.isDigit(key))
 {
   int digit = Character.digit(key, 10); // base 10 numbering
   println("Digit! " + digit);
   
   if (time - lastDigitTime > entrySeparation)
   {
     inputStr = "";
   }
   else if (inputStr.length() > 1)
   {
     inputStr = inputStr.substring(1);
   }
   
   inputStr += key;
   lastDigitTime = time;

   // And now to interpret the "vlaue"
   if (inputStr.length() == 1)
     inputVal = digit * 10;
   else
     inputVal = Integer.parseInt(inputStr);
   
   // Special case: 0 -> 100
   if (inputVal == 0) inputVal = 100;
 }
}
Re: keyboard input, trying to input a two digit number
Reply #7 - Feb 3rd, 2010, 9:39am
 
how can you save multiple frames?
Page Index Toggle Pages: 1