-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathkeyboard.js
929 lines (914 loc) · 21.4 KB
/
keyboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
/**
* @module Events
* @submodule Keyboard
* @for p5
* @requires core
*/
import p5 from '../core/main';
/**
* A `Boolean` system variable that's `true` if any key is currently pressed
* and `false` if not.
*
* @property {Boolean} keyIsPressed
* @readOnly
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a white square at its center. The white square turns black when the user presses a key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* if (keyIsPressed === true) {
* fill(0);
* } else {
* fill(255);
* }
*
* // Draw the square.
* square(25, 25, 50);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a white square at its center. The white square turns black when the user presses a key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* if (keyIsPressed) {
* fill(0);
* } else {
* fill(255);
* }
*
* // Draw the square.
* square(25, 25, 50);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with the word "false" at its center. The word switches to "true" when the user presses a key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER);
* textSize(16);
*
* // Display the value of keyIsPressed.
* text(keyIsPressed, 50, 50);
* }
* </code>
* </div>
*/
p5.prototype.isKeyPressed = false;
p5.prototype.keyIsPressed = false; // khan
/**
* A `String` system variable that contains the value of the last key typed.
*
* The key variable is helpful for checking whether an
* <a href="https://en.wikipedia.org/wiki/ASCII#Printable_characters" target="_blank">ASCII</a>
* key has been typed. For example, the expression `key === "a"` evaluates to
* `true` if the `a` key was typed and `false` if not. `key` doesn’t update
* for special keys such as `LEFT_ARROW` and `ENTER`. Use keyCode instead for
* special keys. The <a href="#/p5/keyIsDown">keyIsDown()</a> function should
* be used to check for multiple different key presses at the same time.
*
* @property {String} key
* @readOnly
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square. The last key pressed is displayed at the center.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER);
* textSize(16);
*
* // Display the last key pressed.
* text(key, 50, 50);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let x = 50;
* let y = 50;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* describe(
* 'A gray square with a black circle at its center. The circle moves when the user presses the keys "w", "a", "s", or "d". It leaves a trail as it moves.'
* );
* }
*
* function draw() {
* // Update x and y if a key is pressed.
* if (keyIsPressed === true) {
* if (key === 'w') {
* y -= 1;
* } else if (key === 's') {
* y += 1;
* } else if (key === 'a') {
* x -= 1;
* } else if (key === 'd') {
* x += 1;
* }
* }
*
* // Style the circle.
* fill(0);
*
* // Draw the circle at (x, y).
* circle(x, y, 5);
* }
* </code>
* </div>
*/
p5.prototype.key = '';
/**
* A `Number` system variable that contains the code of the last key typed.
*
* All keys have a `keyCode`. For example, the `a` key has the `keyCode` 65.
* The `keyCode` variable is helpful for checking whether a special key has
* been typed. For example, the following conditional checks whether the enter
* key has been typed:
*
* ```js
* if (keyCode === 13) {
* // Code to run if the enter key was pressed.
* }
* ```
*
* The same code can be written more clearly using the system variable `ENTER`
* which has a value of 13:
*
* ```js
* if (keyCode === ENTER) {
* // Code to run if the enter key was pressed.
* }
* ```
*
* The system variables `BACKSPACE`, `DELETE`, `ENTER`, `RETURN`, `TAB`,
* `ESCAPE`, `SHIFT`, `CONTROL`, `OPTION`, `ALT`, `UP_ARROW`, `DOWN_ARROW`,
* `LEFT_ARROW`, and `RIGHT_ARROW` are all helpful shorthands the key codes of
* special keys. Key codes can be found on websites such as
* <a href="http://keycode.info/">keycode.info</a>.
*
* @property {Integer} keyCode
* @readOnly
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square. The last key pressed and its code are displayed at the center.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the text.
* textAlign(CENTER);
* textSize(16);
*
* // Display the last key pressed and its code.
* text(`${key} : ${keyCode}`, 50, 50);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let x = 50;
* let y = 50;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* describe(
* '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.'
* );
* }
*
* function draw() {
* // Update x and y if an arrow key is pressed.
* if (keyIsPressed === true) {
* if (keyCode === UP_ARROW) {
* y -= 1;
* } else if (keyCode === DOWN_ARROW) {
* y += 1;
* } else if (keyCode === LEFT_ARROW) {
* x -= 1;
* } else if (keyCode === RIGHT_ARROW) {
* x += 1;
* }
* }
*
* // Style the circle.
* fill(0);
*
* // Draw the circle at (x, y).
* circle(x, y, 5);
* }
* </code>
* </div>
*/
p5.prototype.keyCode = 0;
/**
* A function that's called once when any key is pressed.
*
* Declaring the function `keyPressed()` sets a code block to run once
* automatically when the user presses any key:
*
* ```js
* function keyPressed() {
* // Code to run.
* }
* ```
*
* The <a href="#/p5/key">key</a> and <a href="#/p5/keyCode">keyCode</a>
* variables will be updated with the most recently typed value when
* `keyPressed()` is called by p5.js:
*
* ```js
* function keyPressed() {
* if (key === 'c') {
* // Code to run.
* }
*
* if (keyCode === ENTER) {
* // Code to run.
* }
* }
* ```
*
* The parameter, `event`, is optional. `keyPressed()` is always passed a
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent" target="_blank">KeyboardEvent</a>
* object with properties that describe the key press event:
*
* ```js
* function keyPressed(event) {
* // Code to run that uses the event.
* console.log(event);
* }
* ```
*
* Browsers may have default behaviors attached to various key events. For
* example, some browsers may jump to the bottom of a web page when the
* `SPACE` key is pressed. To prevent any default behavior for this event, add
* `return false;` to the end of the function.
*
* @method keyPressed
* @param {KeyboardEvent} [event] optional `KeyboardEvent` callback argument.
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a black square at its center. The inner square changes color when the user presses a key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Toggle the background color when the user presses a key.
* function keyPressed() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a white square at its center. The inner square turns black when the user presses the "b" key. It turns white when the user presses the "a" key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Reassign value when the user presses the 'a' or 'b' key.
* function keyPressed() {
* if (key === 'a') {
* value = 255;
* } else if (key === 'b') {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a black square at its center. The inner square turns white when the user presses the left arrow key. It turns black when the user presses the right arrow key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Toggle the background color when the user presses an arrow key.
* function keyPressed() {
* if (keyCode === LEFT_ARROW) {
* value = 255;
* } else if (keyCode === RIGHT_ARROW) {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*/
p5.prototype._onkeydown = function(e) {
if (this._downKeys[e.which]) {
// prevent multiple firings
return;
}
this._setProperty('isKeyPressed', true);
this._setProperty('keyIsPressed', true);
this._setProperty('keyCode', e.which);
this._downKeys[e.which] = true;
this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);
const context = this._isGlobal ? window : this;
if (typeof context.keyPressed === 'function' && !e.charCode) {
const executeDefault = context.keyPressed(e);
if (executeDefault === false) {
e.preventDefault();
}
}
};
/**
* A function that's called once when any key is released.
*
* Declaring the function `keyReleased()` sets a code block to run once
* automatically when the user releases any key:
*
* ```js
* function keyReleased() {
* // Code to run.
* }
* ```
*
* The <a href="#/p5/key">key</a> and <a href="#/p5/keyCode">keyCode</a>
* variables will be updated with the most recently released value when
* `keyReleased()` is called by p5.js:
*
* ```js
* function keyReleased() {
* if (key === 'c') {
* // Code to run.
* }
*
* if (keyCode === ENTER) {
* // Code to run.
* }
* }
* ```
*
* The parameter, `event`, is optional. `keyReleased()` is always passed a
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent" target="_blank">KeyboardEvent</a>
* object with properties that describe the key press event:
*
* ```js
* function keyReleased(event) {
* // Code to run that uses the event.
* console.log(event);
* }
* ```
*
* Browsers may have default behaviors attached to various key events. To
* prevent any default behavior for this event, add `return false;` to the end
* of the function.
*
* @method keyReleased
* @param {KeyboardEvent} [event] optional `KeyboardEvent` callback argument.
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a black square at its center. The inner square changes color when the user releases a key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Toggle value when the user releases a key.
* function keyReleased() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a black square at its center. The inner square becomes white when the user releases the "w" key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Set value to 255 the user releases the 'w' key.
* function keyReleased() {
* if (key === 'w') {
* value = 255;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a black square at its center. The inner square turns white when the user presses and releases the left arrow key. It turns black when the user presses and releases the right arrow key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Toggle the background color when the user releases an arrow key.
* function keyReleased() {
* if (keyCode === LEFT_ARROW) {
* value = 255;
* } else if (keyCode === RIGHT_ARROW) {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*/
p5.prototype._onkeyup = function(e) {
this._downKeys[e.which] = false;
if (!this._areDownKeys()) {
this._setProperty('isKeyPressed', false);
this._setProperty('keyIsPressed', false);
}
this._setProperty('_lastKeyCodeTyped', null);
this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);
this._setProperty('keyCode', e.which);
const context = this._isGlobal ? window : this;
if (typeof context.keyReleased === 'function') {
const executeDefault = context.keyReleased(e);
if (executeDefault === false) {
e.preventDefault();
}
}
};
/**
* A function that's called once when keys with printable characters are pressed.
*
* Declaring the function `keyTyped()` sets a code block to run once
* automatically when the user presses any key with a printable character such
* as `a` or 1. Modifier keys such as `SHIFT`, `CONTROL`, and the arrow keys
* will be ignored:
*
* ```js
* function keyTyped() {
* // Code to run.
* }
* ```
*
* The <a href="#/p5/key">key</a> and <a href="#/p5/keyCode">keyCode</a>
* variables will be updated with the most recently released value when
* `keyTyped()` is called by p5.js:
*
* ```js
* function keyTyped() {
* // Check for the "c" character using key.
* if (key === 'c') {
* // Code to run.
* }
*
* // Check for "c" using keyCode.
* if (keyCode === 67) {
* // Code to run.
* }
* }
* ```
*
* The parameter, `event`, is optional. `keyTyped()` is always passed a
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent" target="_blank">KeyboardEvent</a>
* object with properties that describe the key press event:
*
* ```js
* function keyReleased(event) {
* // Code to run that uses the event.
* console.log(event);
* }
* ```
*
* Note: Use the <a href="#/p5/keyPressed">keyPressed()</a> function and
* <a href="#/p5/keyCode">keyCode</a> system variable to respond to modifier
* keys such as `ALT`.
*
* Browsers may have default behaviors attached to various key events. To
* prevent any default behavior for this event, add `return false;` to the end
* of the function.
*
* @method keyTyped
* @param {KeyboardEvent} [event] optional `KeyboardEvent` callback argument.
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
* // Note: Pressing special keys such as SPACE have no effect.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a white square at its center. The inner square changes color when the user presses a key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Toggle the square's color when the user types a printable key.
* function keyTyped() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let value = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* describe(
* 'A gray square with a white square at its center. The inner square turns black when the user types the "b" key. It turns white when the user types the "a" key.'
* );
* }
*
* function draw() {
* background(200);
*
* // Style the square.
* fill(value);
*
* // Draw the square.
* square(25, 25, 50);
* }
*
* // Reassign value when the user types the 'a' or 'b' key.
* function keyTyped() {
* if (key === 'a') {
* value = 255;
* } else if (key === 'b') {
* value = 0;
* }
* // Uncomment to prevent any default behavior.
* // return false;
* }
* </code>
* </div>
*/
p5.prototype._onkeypress = function(e) {
if (e.which === this._lastKeyCodeTyped) {
// prevent multiple firings
return;
}
this._setProperty('_lastKeyCodeTyped', e.which); // track last keyCode
this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);
const context = this._isGlobal ? window : this;
if (typeof context.keyTyped === 'function') {
const executeDefault = context.keyTyped(e);
if (executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The onblur function is called when the user is no longer focused
* on the p5 element. Because the keyup events will not fire if the user is
* not focused on the element we must assume all keys currently down have
* been released.
*/
p5.prototype._onblur = function(e) {
this._downKeys = {};
};
/**
* Returns `true` if the key it’s checking is pressed and `false` if not.
*
* `keyIsDown()` is helpful when checking for multiple different key presses.
* For example, `keyIsDown()` can be used to check if both `LEFT_ARROW` and
* `UP_ARROW` are pressed:
*
* ```js
* if (keyIsDown(LEFT_ARROW) && keyIsDown(UP_ARROW)) {
* // Move diagonally.
* }
* ```
*
* `keyIsDown()` can check for key presses using
* <a href="#/p5/keyCode">keyCode</a> values, as in `keyIsDown(37)` or
* `keyIsDown(LEFT_ARROW)`. Key codes can be found on websites such as
* <a href="https://keycode.info" target="_blank">keycode.info</a>.
*
* @method keyIsDown
* @param {Number} code key to check.
* @return {Boolean} whether the key is down or not.
*
* @example
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let x = 50;
* let y = 50;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* describe(
* '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.'
* );
* }
*
* function draw() {
* // Update x and y if an arrow key is pressed.
* if (keyIsDown(LEFT_ARROW) === true) {
* x -= 1;
* }
*
* if (keyIsDown(RIGHT_ARROW) === true) {
* x += 1;
* }
*
* if (keyIsDown(UP_ARROW) === true) {
* y -= 1;
* }
*
* if (keyIsDown(DOWN_ARROW) === true) {
* y += 1;
* }
*
* // Style the circle.
* fill(0);
*
* // Draw the circle.
* circle(x, y, 5);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let x = 50;
* let y = 50;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* describe(
* '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.'
* );
* }
*
* function draw() {
* // Update x and y if an arrow key is pressed.
* if (keyIsDown(37) === true) {
* x -= 1;
* }
*
* if (keyIsDown(39) === true) {
* x += 1;
* }
*
* if (keyIsDown(38) === true) {
* y -= 1;
* }
*
* if (keyIsDown(40) === true) {
* y += 1;
* }
*
* // Style the circle.
* fill(0);
*
* // Draw the circle.
* circle(x, y, 5);
* }
* </code>
* </div>
*/
p5.prototype.keyIsDown = function(code) {
p5._validateParameters('keyIsDown', arguments);
return this._downKeys[code] || false;
};
/**
* The _areDownKeys function returns a boolean true if any keys pressed
* and a false if no keys are currently pressed.
* Helps avoid instances where multiple keys are pressed simultaneously and
* releasing a single key will then switch the
* keyIsPressed property to true.
* @private
**/
p5.prototype._areDownKeys = function() {
for (const key in this._downKeys) {
if (this._downKeys.hasOwnProperty(key) && this._downKeys[key] === true) {
return true;
}
}
return false;
};
export default p5;