04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Keyboard<br />

Figure A-9. Box Springs sketch<br />

The Keyboard section is similar to the Mouse section in that it combines both system variables<br />

<strong>and</strong> functions that allow you to communicate with the keyboard. The difference<br />

between the mouse <strong>and</strong> the keyboard is the amount <strong>and</strong> type of data input <strong>and</strong> output.<br />

The mouse can have multiple buttons, like a small keyboard, but it also has coordinate<br />

data. The keyboard (at least mine) remains stationary, but has to translate between many<br />

possible input values, for all the letters, numbers, <strong>and</strong> special keys that it uses. The keys on<br />

your keyboard have integer values associated with them, commonly referred to as ASCII<br />

code. For example, the a key equals 65. There are also constant values for the special keys,<br />

such as UP, DOWN, LEFT, RIGHT, ALT/OPTION, CONTROL, SHIFT, BACKSPACE, TAB, ENTER, RETURN,<br />

ESC, <strong>and</strong> DELETE. The following simple example allows you to control a rectangle with the<br />

arrow keys. Notice in the code the use of the if (key == CODED) { } structure around the<br />

specific key detection code.<br />

int x, y;<br />

void setup(){<br />

size(200, 200);<br />

}<br />

void draw() {<br />

stroke(0, 50);<br />

fill(255, 50);<br />

rect(width/2+x, height/2+y, 4+x, 4+y);<br />

}<br />

void keyPressed() {<br />

PROCESSING LANGUAGE API<br />

705<br />

A

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!