6. User Interaction

Cards (15)

  • User input is interaction between the person using the program and the program. This can be entering data in through user input, interacting with the mouse, or actions when certain keys are pressed on the keyboard.
  • The types of data that can be entered are int, float, char, and String.
  • Collect an integer from the user and store it in a variable
    int num = getInt("Put in your integer");
  • Collect a float from the user and store it into a variable
    float value = getFloat("Put in your number");
  • Collect a word from the user and store it into a variable
    String word = getString("Put in your word");
  • Keyboard input looks for the action of pressing a key. It does not send a value in to be stored in a variable, rather it creates an action that calls a keyboard method
  • The procedures for keyboard input are:
    keyPressed() (executed when the key is pressed down)
    keyReleased() (executed when the key is released)
    keyTyped() (executed when a key is pressed and a key is held down(update: called whenever a printable character key (letter, number, punctuation) is pressed))
  • A cod that draws a shape when any key on the keyboard is pressed
    Void keyPressed(){
    circle(400,250,100);
    }
    void draw(){
    }
  • Increases a variable by 2 when ‘w’ is pressed and decreases a variable by 3 when ‘s’ is pressed
    void keyPressed(){
    if(key == 'w'){
    x += 2;
    }
    else if(key == 's'){
    x -= 3;
    }
    }
    void draw(){
    }
  • Multiplies a variable by 4 when the right arrow is pressed, and divides it by 4 when the left arrow is pressed
    void keyPressed(){
    if (key == CODED) {
    if (keyCode == RIGHT) {
    x *= 4;
    }
    else if (keyCode == LEFT) {
    x /= 4;
    }
    }
    void draw(){
    }
  • Mouse input is similar to keyboard input but instead of a code executing when a key is pressed, this is executed when something is done with the mouse.
  • The different procedures for mouse input are:
    mouseDragged() (Code is executed after mouse is moved when there is a button being held down)
    mouseMoved() (Code is executed when the mouse is moved and there isn’t a button being held down)
    mousePressed() (Code executes when a mouse button is pressed)
    mouseReleased() (Code executes when a mouse button is released)
  • A shape at the mouse cursor when the mouse is pressed
    void mousePressed(){
    circle(mouseX,mouseY,100);
    }
    void draw(){
    }
  • A different shape when the mouse is released
    void mouseReleased(){
    square(mouseX,mouseY,100);
    }
    void draw(){
    }
  • A trail of shapes whenever the mouse moves
    void mouseMoved(){
    rect(mouseX,mouseY,200,100);
    }