Skip to content

Commit 2f12bd4

Browse files
authored
Merge pull request #7689 from perminder-17/patch-3
fixing-keyboard.js docs
2 parents e538e51 + c93d7a7 commit 2f12bd4

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/events/keyboard.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,32 +196,36 @@ function keyboard(p5, fn){
196196
fn.key = '';
197197

198198
/**
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.
200200
*
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:
205206
*
206207
* ```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.
209210
* }
210211
* ```
211212
*
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:
214214
*
215215
* ```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.
218218
* }
219219
* ```
220220
*
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
225229
* <a href="http://keycode.info/">keycode.info</a>.
226230
*
227231
* @property {Integer} keyCode
@@ -273,13 +277,13 @@ function keyboard(p5, fn){
273277
* function draw() {
274278
* // Update x and y if an arrow key is pressed.
275279
* if (keyIsPressed === true) {
276-
* if (keyCode === UP_ARROW) {
280+
* if (keyCode === 38) { // Up arrow key
277281
* y -= 1;
278-
* } else if (keyCode === DOWN_ARROW) {
282+
* } else if (keyCode === 40) { // Down arrow key
279283
* y += 1;
280-
* } else if (keyCode === LEFT_ARROW) {
284+
* } else if (keyCode === 37) { // Left arrow key
281285
* x -= 1;
282-
* } else if (keyCode === RIGHT_ARROW) {
286+
* } else if (keyCode === 39) { // Right arrow key
283287
* x += 1;
284288
* }
285289
* }
@@ -317,7 +321,7 @@ function keyboard(p5, fn){
317321
* // Code to run.
318322
* }
319323
*
320-
* if (keyCode === ENTER) {
324+
* if (keyCode === 13) { // Enter key
321325
* // Code to run.
322326
* }
323327
* }
@@ -443,9 +447,9 @@ function keyboard(p5, fn){
443447
*
444448
* // Toggle the background color when the user presses an arrow key.
445449
* function keyPressed() {
446-
* if (keyCode === LEFT_ARROW) {
450+
* if (keyCode === 37) { // Left arrow key
447451
* value = 255;
448-
* } else if (keyCode === RIGHT_ARROW) {
452+
* } else if (keyCode === 39) { // Right arrow key
449453
* value = 0;
450454
* }
451455
* // Uncomment to prevent any default behavior.
@@ -497,7 +501,7 @@ function keyboard(p5, fn){
497501
* // Code to run.
498502
* }
499503
*
500-
* if (keyCode === ENTER) {
504+
* if (keyCode === 13) { // Enter key
501505
* // Code to run.
502506
* }
503507
* }
@@ -620,9 +624,9 @@ function keyboard(p5, fn){
620624
*
621625
* // Toggle the background color when the user releases an arrow key.
622626
* function keyReleased() {
623-
* if (keyCode === LEFT_ARROW) {
627+
* if (keyCode === 37) { // Left arrow key
624628
* value = 255;
625-
* } else if (keyCode === RIGHT_ARROW) {
629+
* } else if (keyCode === 39) { // Right arrow key
626630
* value = 0;
627631
* }
628632
* // Uncomment to prevent any default behavior.
@@ -683,7 +687,7 @@ function keyboard(p5, fn){
683687
* }
684688
*
685689
* // Check for "c" using keyCode.
686-
* if (keyCode === 67) {
690+
* if (keyCode === 67) { // 67 is the ASCII code for 'c'
687691
* // Code to run.
688692
* }
689693
* }
@@ -899,19 +903,19 @@ function keyboard(p5, fn){
899903
*
900904
* function draw() {
901905
* // Update x and y if an arrow key is pressed.
902-
* if (keyIsDown(37) === true) {
906+
* if (keyIsDown('ArrowLeft') === true) {
903907
* x -= 1;
904908
* }
905909
*
906-
* if (keyIsDown(39) === true) {
910+
* if (keyIsDown('ArrowRight') === true) {
907911
* x += 1;
908912
* }
909913
*
910-
* if (keyIsDown(38) === true) {
914+
* if (keyIsDown('ArrowUp') === true) {
911915
* y -= 1;
912916
* }
913917
*
914-
* if (keyIsDown(40) === true) {
918+
* if (keyIsDown('ArrowDown') === true) {
915919
* y += 1;
916920
* }
917921
*

0 commit comments

Comments
 (0)