Skip to content

Commit 19e74c1

Browse files
committed
adding docs for code
1 parent 3eae276 commit 19e74c1

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

src/events/keyboard.js

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ function keyboard(p5, fn){
111111
*/
112112

113113
fn.keyIsPressed = false;
114-
fn.code = null;
115114

116115
/**
117116
* A `String` system variable that contains the value of the last key typed.
@@ -195,6 +194,117 @@ function keyboard(p5, fn){
195194
*/
196195
fn.key = '';
197196

197+
/**
198+
* The `code` property represents a physical key on the keyboard (as opposed
199+
* to the character generated by pressing the key). In other words, this
200+
* property returns a value that isn't altered by keyboard layout or the state
201+
* of the modifier keys.
202+
*
203+
* This property is useful when you want to handle keys based on their
204+
* physical positions on the input device rather than the characters associated
205+
* with those keys;
206+
*
207+
* Unlike <a href="#/p5/key">key</a>, the `code` property differentiates between
208+
* physical keys that generate the same character—for example, `CtrlLeft` and
209+
* `CtrlRight`—so each can be handled independently.
210+
*
211+
* Pressing the key physically labeled “A” always yields `KeyA`, regardless
212+
* of the current keyboard layout (QWERTY, Dvorak, AZERTY, etc.) or the character
213+
* that appears in a text field.
214+
*
215+
* The code property returns a plain string (e.g. 'ArrowRight'), you can
216+
* compare it directly with string literals:
217+
* ```js
218+
* if (code === 'ArrowRight') {
219+
* // …
220+
* }
221+
* ```
222+
*
223+
* For extra clarity, you can instead use the predefined key-code constants
224+
* provided by p5.js—`BACKSPACE`, `DELETE`, `ENTER`, `RETURN`, `TAB`, `ESCAPE`, `SHIFT`,
225+
* `CONTROL`, `OPTION`, `ALT`, `UP_ARROW`, `DOWN_ARROW`, `LEFT_ARROW`, and `RIGHT_ARROW`.
226+
* These are simply shorthands for the same string values:
227+
* ```js
228+
* if (code === RIGHT_ARROW) {
229+
* // ..
230+
* }
231+
* ```
232+
*
233+
* @property {String} code
234+
* @readOnly
235+
*
236+
* @example
237+
* <div>
238+
* <code>
239+
* // Click on the canvas to begin detecting key presses.
240+
*
241+
* function setup() {
242+
* createCanvas(100, 100);
243+
*
244+
* describe(
245+
* 'A gray square. The last key pressed is displayed at the center.'
246+
* );
247+
* }
248+
*
249+
* function draw() {
250+
* background(200);
251+
*
252+
* // Style the text.
253+
* textAlign(CENTER);
254+
* textSize(16);
255+
*
256+
* // Display the last key pressed.
257+
* text(code, 50, 50);
258+
* }
259+
* </code>
260+
* </div>
261+
*
262+
* <div>
263+
* <code>
264+
* // Click on the canvas to begin detecting key presses.
265+
*
266+
* let x = 50;
267+
* let y = 50;
268+
*
269+
* function setup() {
270+
* createCanvas(100, 100);
271+
*
272+
* background(200);
273+
*
274+
* describe(
275+
* 'A gray square with a black circle at its center. The circle moves when the user presses an arrow key. It leaves a trail as it moves.'
276+
* );
277+
* }
278+
*
279+
* function draw() {
280+
* // Update x and y if an arrow key is pressed.
281+
* if (code === LEFT_ARROW) {
282+
* x -= 1;
283+
* }
284+
*
285+
* if (code === RIGHT_ARROW) {
286+
* x += 1;
287+
* }
288+
*
289+
* if (code === UP_ARROW) {
290+
* y -= 1;
291+
* }
292+
*
293+
* if (code === DOWN_ARROW) {
294+
* y += 1;
295+
* }
296+
*
297+
* // Style the circle.
298+
* fill(0);
299+
*
300+
* // Draw the circle.
301+
* circle(x, y, 5);
302+
* }
303+
* </code>
304+
* </div>
305+
*/
306+
fn.code = '';
307+
198308
/**
199309
* A `Number` system variable that contains the code of the last key pressed.
200310
*
@@ -652,7 +762,7 @@ function keyboard(p5, fn){
652762
if (!this._areDownKeys()) {
653763
this.keyIsPressed = false;
654764
this.key = '';
655-
this.code = null;
765+
this.code = '';
656766
} else {
657767
// If other keys are still pressed, update code to the last pressed key
658768
const lastPressedCode = Object.keys(this._downKeyCodes).pop();

0 commit comments

Comments
 (0)