@@ -207,6 +207,8 @@ function keyboard(p5, fn){
207
207
* Unlike <a href="#/p5/key">key</a>, the `code` property differentiates between
208
208
* physical keys that generate the same character—for example, `CtrlLeft` and
209
209
* `CtrlRight`—so each can be handled independently.
210
+ * Here's the MDN docs for <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code" target="_blank">KeyboardEvent.code</a>
211
+
210
212
*
211
213
* Pressing the key physically labeled “A” always yields `KeyA`, regardless
212
214
* of the current keyboard layout (QWERTY, Dvorak, AZERTY, etc.) or the character
@@ -215,21 +217,32 @@ function keyboard(p5, fn){
215
217
* The code property returns a plain string (e.g., 'ArrowRight'). You can
216
218
* compare it directly with string literals:
217
219
* ```js
218
- * if (code === 'ArrowRight') {
219
- * // …
220
+ * if (keyIsDown(RIGHT_ARROW)) {
221
+ * // …
222
+ * }
223
+ * // The line above is equivalent to:
224
+ * if (code === 'ArrowRight') {
225
+ * // …
226
+ * }
227
+ * ```
228
+ * if (key === 'ArrowRight') {
229
+ * // …
220
230
* }
221
231
* ```
222
232
*
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`.
233
+ * The system variables `BACKSPACE`, `DELETE`, `ENTER`, `RETURN`, `TAB`,
234
+ * `ESCAPE`, `SHIFT`, `CONTROL`, `OPTION`, `ALT`, `UP_ARROW`, `DOWN_ARROW`,
235
+ * `LEFT_ARROW`, and `RIGHT_ARROW` are all helpful shorthands the key codes of
236
+ * special keys.
226
237
* These are simply shorthands for the same string values:
227
238
* ```js
228
239
* if (code === RIGHT_ARROW) {
229
240
* // ..
230
241
* }
231
242
* ```
232
243
*
244
+ *
245
+ *
233
246
* @property {String } code
234
247
* @readOnly
235
248
*
@@ -258,48 +271,77 @@ function keyboard(p5, fn){
258
271
* }
259
272
* </code>
260
273
* </div>
274
+ * <div>
275
+ * <code>
276
+ *
277
+ * function setup() {
278
+ * createCanvas(100, 100);
279
+ * }
280
+ *
281
+ * function draw() {
282
+ * background(220);
283
+ * fill("black");
284
+ * if (keyIsDown(BACKSPACE) || keyIsDown(ENTER) ||
285
+ * keyIsDown(DELETE) || keyIsDown(RETURN) ||
286
+ * keyIsDown(TAB) || keyIsDown(ESCAPE) ||
287
+ * keyIsDown(CONTROL) || keyIsDown(OPTION) ||
288
+ * keyIsDown(UP_ARROW) || keyIsDown(LEFT_ARROW) ||
289
+ * keyIsDown(RIGHT_ARROW) || keyIsDown(DOWN_ARROW) ||
290
+ * keyIsDown(SHIFT)) {
291
+ * fill("red");
292
+ * text("System Variable", 7, 75);
293
+ * }
261
294
*
295
+ * text(key, 30, 25);
296
+ * text(keyCode, 7, 25);
297
+ * text(code || " ", 30, 50);
298
+ * }
299
+ * </div>
300
+ * </code>
262
301
* <div>
263
302
* <code>
264
303
* // Click on the canvas to begin detecting key presses.
265
304
*
266
305
* let x = 50;
267
306
* let y = 50;
268
- *
307
+ *
269
308
* function setup() {
270
309
* createCanvas(100, 100);
271
- *
310
+ *
272
311
* background(200);
273
- *
312
+ *
274
313
* describe(
275
314
* '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
315
* );
277
316
* }
278
- *
317
+ *
279
318
* function draw() {
280
319
* // 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;
320
+ * if (keyIsPressed){
321
+ * if (keyIsDown(LEFT_ARROW)){
322
+ * x -= 1;
323
+ * }
324
+ *
325
+ * if (keyIsDown(RIGHT_ARROW)) {
326
+ * x += 1;
327
+ * }
328
+ *
329
+ * if (keyIsDown(UP_ARROW)) {
330
+ * y -= 1;
331
+ * }
332
+ *
333
+ * if (keyIsDown(DOWN_ARROW)) {
334
+ * y += 1;
335
+ * }
295
336
* }
296
- *
337
+ *
297
338
* // Style the circle.
298
339
* fill(0);
299
- *
340
+ *
300
341
* // Draw the circle.
301
342
* circle(x, y, 5);
302
343
* }
344
+ *
303
345
* </code>
304
346
* </div>
305
347
*/
0 commit comments