Skip to content

Commit e8cd05c

Browse files
authored
fixing as per suggestions.
1 parent 7f57497 commit e8cd05c

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

src/events/keyboard.js

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ function keyboard(p5, fn){
207207
* Unlike <a href="#/p5/key">key</a>, the `code` property differentiates between
208208
* physical keys that generate the same character—for example, `CtrlLeft` and
209209
* `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+
210212
*
211213
* Pressing the key physically labeled “A” always yields `KeyA`, regardless
212214
* of the current keyboard layout (QWERTY, Dvorak, AZERTY, etc.) or the character
@@ -215,21 +217,32 @@ function keyboard(p5, fn){
215217
* The code property returns a plain string (e.g., 'ArrowRight'). You can
216218
* compare it directly with string literals:
217219
* ```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+
* // …
220230
* }
221231
* ```
222232
*
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.
226237
* These are simply shorthands for the same string values:
227238
* ```js
228239
* if (code === RIGHT_ARROW) {
229240
* // ..
230241
* }
231242
* ```
232243
*
244+
*
245+
*
233246
* @property {String} code
234247
* @readOnly
235248
*
@@ -258,48 +271,77 @@ function keyboard(p5, fn){
258271
* }
259272
* </code>
260273
* </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+
* }
261294
*
295+
* text(key, 30, 25);
296+
* text(keyCode, 7, 25);
297+
* text(code || " ", 30, 50);
298+
* }
299+
* </div>
300+
* </code>
262301
* <div>
263302
* <code>
264303
* // Click on the canvas to begin detecting key presses.
265304
*
266305
* let x = 50;
267306
* let y = 50;
268-
*
307+
*
269308
* function setup() {
270309
* createCanvas(100, 100);
271-
*
310+
*
272311
* background(200);
273-
*
312+
*
274313
* describe(
275314
* '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.'
276315
* );
277316
* }
278-
*
317+
*
279318
* function draw() {
280319
* // 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+
* }
295336
* }
296-
*
337+
*
297338
* // Style the circle.
298339
* fill(0);
299-
*
340+
*
300341
* // Draw the circle.
301342
* circle(x, y, 5);
302343
* }
344+
*
303345
* </code>
304346
* </div>
305347
*/

0 commit comments

Comments
 (0)