@@ -196,32 +196,36 @@ function keyboard(p5, fn){
196
196
fn . key = '' ;
197
197
198
198
/**
199
- * A `Number` system variable that contains the code of the last key typed .
199
+ * A `Number` system variable that contains the code of the last key pressed .
200
200
*
201
- * All keys have a `keyCode`. For example, the `a` key has the `keyCode` 65.
202
- * The `keyCode` variable is helpful for checking whether a special key has
203
- * been typed. For example, the following conditional checks whether the enter
204
- * key has been typed:
201
+ * Every key has a numeric key code. For example, the letter `a` key has the key code 65.
202
+ * Use this key code to determine which key was pressed by comparing it to the numeric value
203
+ * of the desired key.
204
+ *
205
+ * For example, to detect when the Enter key is pressed:
205
206
*
206
207
* ```js
207
- * if (keyCode === 13) {
208
- * // Code to run if the enter key was pressed.
208
+ * if (keyCode === 13) { // Enter key
209
+ * // Code to run if the Enter key was pressed.
209
210
* }
210
211
* ```
211
212
*
212
- * The same code can be written more clearly using the system variable `ENTER`
213
- * which has a value of 13:
213
+ * Alternatively, you can use the <a href="#/p5/key">key</a> function to directly compare the key value:
214
214
*
215
215
* ```js
216
- * if (keyCode === ENTER ) {
217
- * // Code to run if the enter key was pressed.
216
+ * if (key === 'Enter' ) { // Enter key
217
+ * // Code to run if the Enter key was pressed.
218
218
* }
219
219
* ```
220
220
*
221
- * The system variables `BACKSPACE`, `DELETE`, `ENTER`, `RETURN`, `TAB`,
222
- * `ESCAPE`, `SHIFT`, `CONTROL`, `OPTION`, `ALT`, `UP_ARROW`, `DOWN_ARROW`,
223
- * `LEFT_ARROW`, and `RIGHT_ARROW` are all helpful shorthands the key codes of
224
- * special keys. Key codes can be found on websites such as
221
+ * Use the following numeric codes for the arrow keys:
222
+ *
223
+ * Up Arrow: 38
224
+ * Down Arrow: 40
225
+ * Left Arrow: 37
226
+ * Right Arrow: 39
227
+ *
228
+ * More key codes can be found at websites such as
225
229
* <a href="http://keycode.info/">keycode.info</a>.
226
230
*
227
231
* @property {Integer } keyCode
@@ -273,13 +277,13 @@ function keyboard(p5, fn){
273
277
* function draw() {
274
278
* // Update x and y if an arrow key is pressed.
275
279
* if (keyIsPressed === true) {
276
- * if (keyCode === UP_ARROW ) {
280
+ * if (keyCode === 38 ) { // Up arrow key
277
281
* y -= 1;
278
- * } else if (keyCode === DOWN_ARROW ) {
282
+ * } else if (keyCode === 40 ) { // Down arrow key
279
283
* y += 1;
280
- * } else if (keyCode === LEFT_ARROW ) {
284
+ * } else if (keyCode === 37 ) { // Left arrow key
281
285
* x -= 1;
282
- * } else if (keyCode === RIGHT_ARROW ) {
286
+ * } else if (keyCode === 39 ) { // Right arrow key
283
287
* x += 1;
284
288
* }
285
289
* }
@@ -317,7 +321,7 @@ function keyboard(p5, fn){
317
321
* // Code to run.
318
322
* }
319
323
*
320
- * if (keyCode === ENTER ) {
324
+ * if (keyCode === 13 ) { // Enter key
321
325
* // Code to run.
322
326
* }
323
327
* }
@@ -443,9 +447,9 @@ function keyboard(p5, fn){
443
447
*
444
448
* // Toggle the background color when the user presses an arrow key.
445
449
* function keyPressed() {
446
- * if (keyCode === LEFT_ARROW ) {
450
+ * if (keyCode === 37 ) { // Left arrow key
447
451
* value = 255;
448
- * } else if (keyCode === RIGHT_ARROW ) {
452
+ * } else if (keyCode === 39 ) { // Right arrow key
449
453
* value = 0;
450
454
* }
451
455
* // Uncomment to prevent any default behavior.
@@ -497,7 +501,7 @@ function keyboard(p5, fn){
497
501
* // Code to run.
498
502
* }
499
503
*
500
- * if (keyCode === ENTER ) {
504
+ * if (keyCode === 13 ) { // Enter key
501
505
* // Code to run.
502
506
* }
503
507
* }
@@ -620,9 +624,9 @@ function keyboard(p5, fn){
620
624
*
621
625
* // Toggle the background color when the user releases an arrow key.
622
626
* function keyReleased() {
623
- * if (keyCode === LEFT_ARROW ) {
627
+ * if (keyCode === 37 ) { // Left arrow key
624
628
* value = 255;
625
- * } else if (keyCode === RIGHT_ARROW ) {
629
+ * } else if (keyCode === 39 ) { // Right arrow key
626
630
* value = 0;
627
631
* }
628
632
* // Uncomment to prevent any default behavior.
@@ -683,7 +687,7 @@ function keyboard(p5, fn){
683
687
* }
684
688
*
685
689
* // Check for "c" using keyCode.
686
- * if (keyCode === 67) {
690
+ * if (keyCode === 67) { // 67 is the ASCII code for 'c'
687
691
* // Code to run.
688
692
* }
689
693
* }
@@ -899,19 +903,19 @@ function keyboard(p5, fn){
899
903
*
900
904
* function draw() {
901
905
* // Update x and y if an arrow key is pressed.
902
- * if (keyIsDown(37 ) === true) {
906
+ * if (keyIsDown('ArrowLeft' ) === true) {
903
907
* x -= 1;
904
908
* }
905
909
*
906
- * if (keyIsDown(39 ) === true) {
910
+ * if (keyIsDown('ArrowRight' ) === true) {
907
911
* x += 1;
908
912
* }
909
913
*
910
- * if (keyIsDown(38 ) === true) {
914
+ * if (keyIsDown('ArrowUp' ) === true) {
911
915
* y -= 1;
912
916
* }
913
917
*
914
- * if (keyIsDown(40 ) === true) {
918
+ * if (keyIsDown('ArrowDown' ) === true) {
915
919
* y += 1;
916
920
* }
917
921
*
0 commit comments