Commands / add()

// Be sure to click the applet so it knows about your keyboard
import compose.commands.*;

Commands commands;

void setup() {
  commands = new Commands(this);
  commands.add("drawEllipse", 'd');
  commands.add("drawRect", CONTROL, 'd');
}

// You must add draw, or Command won't receive key events!
void draw() { }

void drawEllipse() {
  ellipse(random(width), random(height), 10, 10);
}

void drawRect() {
  rect(random(width), random(height), 20, 20);
}

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Description

Ties a key or a keystroke to a function, as an alternative to having a large if-else statement in your keyPressed() method. The function you assign to the key or keystroke must have no parameters. You can specify as many keyCodes as you want.

Syntax

commands.add(functionName, keyCode)    
commands.add(functionName, keyCode, keyCode)
commands.add(functionName, keyCode, keyCode, keyCode) ...

commands.add(functionName, target, keyCode)
commands.add(functionName, target, keyCode, keyCode)
commands.add(functionName, target, keyCode, keyCode, keyCode) ...
Parameters
functionName
String: the name of the function to be called
keyCode
char or int: the key that calls the function.
target
Object: the object the function belongs to (assumed to be PApplet)
Returns
None
Usage
Web & Applications
Related
Last Modified: March 3, 2010

1 Comment

  1. admin
    Wed, 03 Mar 10 15:22:16 -0800 #

    question~?

You must be logged in to post a comment.