home/ atoms/ processing-keyboard-input

Processing keyboard events use keyPressed and key variables, with keyCode for special keys

Processing makes keyboard input available in two ways. The variable keyPressed is true while any key is held and can be tested inside draw(). The variable key holds the most recently pressed character. For special keys (arrows, function keys), key is set to CODED and the actual key is identified via keyCode (UP, DOWN, LEFT, RIGHT, etc.). Event functions keyPressed() and keyReleased() run once per key event. The keyboard’s position on a grid can be used as a numeric input system independent of the printed characters.

Examples

void keyPressed() { if (key == ‘r’) background(255,0,0); if (keyCode == UP) y -= 5; if (keyCode == DOWN) y += 5; }

Assessment

Write a sketch where arrow keys move a circle across the canvas; add a boundary check so the circle wraps around from one edge to the other.

“When writing your own software, you have the freedom to use the keyboard data any way you wish.”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 50