-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixing-keyboard.js docs #7689
fixing-keyboard.js docs #7689
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,32 +196,36 @@ function keyboard(p5, fn){ | |
fn.key = ''; | ||
|
||
/** | ||
* A `Number` system variable that contains the code of the last key typed. | ||
* A `Number` system variable that contains the code of the last key pressed. | ||
* | ||
* 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: | ||
* Every key has a numeric key code. For example, the letter `a` key has the key code 65. | ||
* Use this key code to determine which key was pressed by comparing it to the numeric value | ||
* of the desired key. | ||
* | ||
* For example, to detect when the Enter key is pressed: | ||
* | ||
* ```js | ||
* if (keyCode === 13) { | ||
* // Code to run if the enter key was pressed. | ||
* if (keyCode === 13) { // Enter key | ||
* // 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: | ||
* Alternatively, you can use the <a href="#/p5/key">key</a> function to directly compare the key value: | ||
* | ||
* ```js | ||
* if (keyCode === ENTER) { | ||
* // Code to run if the enter key was pressed. | ||
* if (key === 'Enter') { // Enter key | ||
* // 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 | ||
* Use the following numeric codes for the arrow keys: | ||
* | ||
* Up Arrow: 38 | ||
* Down Arrow: 40 | ||
* Left Arrow: 37 | ||
* Right Arrow: 39 | ||
* | ||
* More key codes can be found at websites such as | ||
* <a href="http://keycode.info/">keycode.info</a>. | ||
* | ||
* @property {Integer} keyCode | ||
|
@@ -273,13 +277,13 @@ function keyboard(p5, fn){ | |
* function draw() { | ||
* // Update x and y if an arrow key is pressed. | ||
* if (keyIsPressed === true) { | ||
* if (keyCode === UP_ARROW) { | ||
* if (keyCode === 38) { // Up arrow key | ||
* y -= 1; | ||
* } else if (keyCode === DOWN_ARROW) { | ||
* } else if (keyCode === 40) { // Down arrow key | ||
* y += 1; | ||
* } else if (keyCode === LEFT_ARROW) { | ||
* } else if (keyCode === 37) { // Left arrow key | ||
* x -= 1; | ||
* } else if (keyCode === RIGHT_ARROW) { | ||
* } else if (keyCode === 39) { // Right arrow key | ||
* x += 1; | ||
* } | ||
* } | ||
|
@@ -317,7 +321,7 @@ function keyboard(p5, fn){ | |
* // Code to run. | ||
* } | ||
* | ||
* if (keyCode === ENTER) { | ||
* if (keyCode === 13) { // Enter key | ||
* // Code to run. | ||
* } | ||
* } | ||
|
@@ -443,9 +447,9 @@ function keyboard(p5, fn){ | |
* | ||
* // Toggle the background color when the user presses an arrow key. | ||
* function keyPressed() { | ||
* if (keyCode === LEFT_ARROW) { | ||
* if (keyCode === 37) { // Left arrow key | ||
* value = 255; | ||
* } else if (keyCode === RIGHT_ARROW) { | ||
* } else if (keyCode === 39) { // Right arrow key | ||
* value = 0; | ||
* } | ||
* // Uncomment to prevent any default behavior. | ||
|
@@ -497,7 +501,7 @@ function keyboard(p5, fn){ | |
* // Code to run. | ||
* } | ||
* | ||
* if (keyCode === ENTER) { | ||
* if (keyCode === 13) { // Enter key | ||
* // Code to run. | ||
* } | ||
* } | ||
|
@@ -620,9 +624,9 @@ function keyboard(p5, fn){ | |
* | ||
* // Toggle the background color when the user releases an arrow key. | ||
* function keyReleased() { | ||
* if (keyCode === LEFT_ARROW) { | ||
* if (keyCode === 37) { // Left arrow key | ||
* value = 255; | ||
* } else if (keyCode === RIGHT_ARROW) { | ||
* } else if (keyCode === 39) { // Right arrow key | ||
* value = 0; | ||
* } | ||
* // Uncomment to prevent any default behavior. | ||
|
@@ -683,7 +687,7 @@ function keyboard(p5, fn){ | |
* } | ||
* | ||
* // Check for "c" using keyCode. | ||
* if (keyCode === 67) { | ||
* if (keyCode === 67) { // 67 is the ASCII code for 'c' | ||
* // Code to run. | ||
* } | ||
* } | ||
|
@@ -899,19 +903,19 @@ function keyboard(p5, fn){ | |
* | ||
* function draw() { | ||
* // Update x and y if an arrow key is pressed. | ||
* if (keyIsDown(37) === true) { | ||
* if (keyIsDown('ArrowLeft') === true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while this is correct and same from the old version, I think its a pattern we should discourage, maybe add a note at the begining that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would support simplifying this as you suggest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I generally also agree, but also wanted to flag that this is consistent with what the current documentation style guide has, so we might want to also update that if we're considering changing the pattern we want to use. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it’s valuable to maintain the explicit comparison with Currently we could keep this as it is and in future if it comes up, I would be more than happy to change the style-guide and replace by not comparing with truth values? Let me know your thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think at this point it's ok to leave as-is because probably other things are priority if that's ok with everyone else. But maybe we can make an issue to discuss changing the style guide + updating other docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, its a matter of style, and if the explicit type comparison to true makes sense in this context lets keep it. |
||
* x -= 1; | ||
* } | ||
* | ||
* if (keyIsDown(39) === true) { | ||
* if (keyIsDown('ArrowRight') === true) { | ||
* x += 1; | ||
* } | ||
* | ||
* if (keyIsDown(38) === true) { | ||
* if (keyIsDown('ArrowUp') === true) { | ||
* y -= 1; | ||
* } | ||
* | ||
* if (keyIsDown(40) === true) { | ||
* if (keyIsDown('ArrowDown') === true) { | ||
* y += 1; | ||
* } | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if a
select(key)
would be better or will it cause confusion for some users? We could simplify the if else logic, what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok leaving this one as is for now since after a quick search, we don't use
switch
/case
in other reference examples so far, so while it can be simplified, it might be a little easier to understand for now if we use the more familiarif/
else` construct.