Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 18-1: User input

PFont f;

// Variable to store text currently being typed
String typing = "";

// Variable to store saved text when return is hit
String saved = "";

void setup() {
  size(300,200);
  f = createFont("Arial",16,true);
}

void draw() {
  background(255);
  int indent = 25;
  
  // Set the font and fill for text
  textFont(f);
  fill(0);
  
  // Display everything
  text("Click in this applet and type. \nHit return to save what you typed. ", indent, 40);
  text(typing,indent,90);
  text(saved,indent,130);
}

void keyPressed() {
  // If the return key is pressed, save the String and clear it
  if (key == '\n' ) {
    saved = typing;
    // A String can be cleared by setting it equal to ""
    typing = ""; 
  } else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key; 
  }
}
  • Pingback: 3 New (and Nerdy) Things to Know in ‘09 | Fresh

  • http://www.watch-d-movie.de/ mr.winkle

    Thank you so much for this!
    For those wondering about deleting mistyped letters insted of

    typing = typing + key;

    use this:

    if (keyCode == BACKSPACE) {
    typing = typing.substring(0, typing.length() – 1);
    }
    else
    if (key != CODED) typing += key;

    greets, mr.winkle

  • Josemg14

    I love processing and your examples are the best

  • Douglas

    Hi, Thanks for the code. :)

    Is there a way of exporting each of the saved lines to a text file or similar? I want to automate a process of typing a line then programming the text onto a LCD board to be displayed.

    Thanks

  • shiffman

    check out PrintWriter or saveStrings()

  • EdMaxPrime

    When I tried this in my code it adds the key code to the string instead of an alphanumeric character.

  • Giaky

    I have a question, if I enter only numbers, can i transform the string in a float that i can use later?

  • shiffman

    yes.

    float num = float(typing);

  • jlpeacock

    Having the same issue when running the sketch in javascript mode, Thoughts @shiffman:disqus?

  • jlpeacock

    Seems in order for this to work in pjs you need to use key.toString() instead of just key. See this example http://sketchpad.cc/efRbqG9qUn

  • jlpeacock

    Thank Mr.Winkle ;)

    I needed to use length -2 to get this working.

    if (keyCode == BACKSPACE) {
    if (typing.length() > 1){
    typing = typing.substring(0, typing.length()-2);
    }
    } else {
    typing = typing;
    }
    }

  • Bob Denver

    I’m running a processing.js sketch with this code. It works fine except if I try to access the sketch via mobile device. No keyboard comes up on the mobile device so I can’t enter text. Anyone know how to get input to work on mobile?

  • http://www.manimalcollective.com Abrar Burk

    hey! thanks so much! so is there anyway wherein, i can trigger off a video when a user types in a particular string, say ‘apple’ ? please help!

  • Remmert

    A really nice example, learned a lot of it. Here is my one version of it, a few more saves and working backspace:

    // Learning Processing
    // Daniel Shiffman

    // http://www.learningprocessing.com
    // Example 18-1: User input

    //Edited by Remmert Scholte
    PFont f;

    // Variable to store text currently being typed

    String typing = “”;

    // Variable to store saved text when return is hit
    String saved = “”;
    String saved1 = “”;
    String saved2 = “”;
    String saved3 = “”;
    void setup() {
    size(300,400);
    f = createFont(“Arial”,16,true);

    }

    void draw() {
    background(255);
    int indent = 25;

    // Set the font and fill for text
    textFont(f);
    fill(0);

    // Display everything
    text(“Click in this applet and type. nHit return to save what you typed. “, indent, 40);
    text(typing,indent,90);
    text(saved,indent,130);
    text(saved1,indent,145);
    text(saved2,indent,160);
    text(saved3,indent,175);

    }

    void keyPressed() {
    // If the return key is pressed, save the String and clear it
    if (key == ‘n’ ) {
    saved3 = saved2;
    saved2 = saved1;
    saved1 = saved;
    saved = typing;
    // A String can be cleared by setting it equal to “”
    typing = “”;
    } else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    if (key == BACKSPACE && typing.length() > 0){
    typing = typing.substring(0,typing.length()-1);
    }else if (key != BACKSPACE){
    typing = typing + key;
    }
    }
    }