/* 6.914, Example 1.8 char, key, keyPressed, and keyReleased 0) char is a data type that uses a single character. You can distinguish char from strings (used with print and println) because chars have single quotes rather than double quotes and can have only one character. 1) You can use keyPressed like mousePressed, as a boolean. When a key is pressed, it changes the value of 'key', which is an element of type char. You can check whether a particular character has been pressed by using the 'is equal to?' notation, ==. When you want to check whether any key in a set of keys (for instance, 'e' or 'E') has been pressed, use || - 'or' in boolean logic. It can also be used in its own method, which will be called whenever a key is pressed. 2) Some keys are in the ASCII key set, so you can check them just like normal keys, using the == notation. These keys are BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE. Other keys are not in the ASCII key set. These keys are the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT. They must be referenced by using keyCode. Before looking for a keyCode, check whether your key is actually coded: void keyPressed(){ if (key == CODED){ if (keyCode == UP){ println("Up was pressed"); } } } 3) As with mouseReleased, the keyReleased idea is a method but not a boolean. It will be called whenever a pressed key is released, and the key that was released will be saved to the 'key' character. */ void setup(){ size(100, 100); frameRate(2); } void draw(){ if (keyPressed){ if (key == 'e' || key == 'E'){ println("E/e has been pressed!"); } else if (key == 'c'){ ellipse(50, 50, 10, 10); } else { println("a non-specific key was pressed."); } } } void keyReleased(){ println(key+" was just released!"); }