@@ -111,7 +111,6 @@ function keyboard(p5, fn){
111111 */
112112
113113 fn . keyIsPressed = false ;
114- fn . code = null ;
115114
116115 /**
117116 * A `String` system variable that contains the value of the last key typed.
@@ -195,6 +194,191 @@ function keyboard(p5, fn){
195194 */
196195 fn . key = '' ;
197196
197+ /**
198+ * The `code` property represents a physical key on the keyboard (as opposed
199+ * to the character generated by pressing the key). In other words, this
200+ * property returns a value that isn't altered by keyboard layout or the state
201+ * of the modifier keys.
202+ *
203+ * This property is useful when you want to handle keys based on their
204+ * physical positions on the input device rather than the characters associated
205+ * with those keys;
206+ *
207+ * Unlike <a href="#/p5/key">key</a>, the `code` property differentiates between
208+ * physical keys that generate the same character—for example, `CtrlLeft` and
209+ * `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+
212+ *
213+ * Pressing the key physically labeled “A” always yields `KeyA`, regardless
214+ * of the current keyboard layout (QWERTY, Dvorak, AZERTY, etc.) or the character
215+ * that appears in a text field.
216+ *
217+ * The code property returns a plain string (e.g., 'ArrowRight'). You can
218+ * compare it directly with string literals:
219+ * ```js
220+ * if (keyIsDown(RIGHT_ARROW)) {
221+ * // …
222+ * }
223+ * // The line above is equivalent to:
224+ * if (code === 'ArrowRight') {
225+ * // …
226+ * }
227+ * if (key === 'ArrowRight') {
228+ * // …
229+ * }
230+ * ```
231+ *
232+ * The system variables `BACKSPACE`, `DELETE`, `ENTER`, `RETURN`, `TAB`,
233+ * `ESCAPE`, `SHIFT`, `CONTROL`, `OPTION`, `ALT`, `UP_ARROW`, `DOWN_ARROW`,
234+ * `LEFT_ARROW`, and `RIGHT_ARROW` are all helpful shorthands the key codes of
235+ * special keys.
236+ * These are simply shorthands for the same string values:
237+ * ```js
238+ * if (code === RIGHT_ARROW) {
239+ * // ..
240+ * }
241+ * ```
242+ *
243+ *
244+ * <p>The table below summarizes how the main keyboard-related system variables changed between p5.js 1.x and 2.x.</p>
245+ * <table>
246+ * <thead>
247+ * <tr>
248+ * <th>Variable</th>
249+ * <th>p5.js 1.x </th>
250+ * <th>p5.js 2.x </th>
251+ * </tr>
252+ * </thead>
253+ * <tbody>
254+ * <tr>
255+ * <td><code>key</code></td>
256+ * <td>Text string (e.g., <code>"ArrowUp"</code>).</td>
257+ * <td>Text string (e.g., <code>"ArrowUp"</code>, <code>"f"</code> or <code>"F"</code>).</td>
258+ * </tr>
259+ * <tr>
260+ * <td><code>code</code></td>
261+ * <td><em>Not supported.</em></td>
262+ * <td>Text String (e.g., <code>"ArrowUp"</code>, <code>"KeyF"</code>).</td>
263+ * </tr>
264+ * <tr>
265+ * <td><code>keyCode</code></td>
266+ * <td>Number (e.g., <code>70</code>).</td>
267+ * <td>Number (unchanged; e.g., <code>70</code>).</td>
268+ * </tr>
269+ * <tr>
270+ * <td>System variables (<code>BACKSPACE</code>, <code>UP_ARROW</code>, …)</td>
271+ * <td>Number</td>
272+ * <td>Text String (e.g., <code>"ArrowUp"</code>).</td>
273+ * </tr>
274+ * </tbody>
275+ * </table>
276+ *
277+ *
278+ * @property {String } code
279+ * @readOnly
280+ *
281+ * @example
282+ * <div>
283+ * <code>
284+ * // Click on the canvas to begin detecting key presses.
285+ *
286+ * function setup() {
287+ * createCanvas(100, 100);
288+ *
289+ * describe(
290+ * 'A gray square. The last key pressed is displayed at the center.'
291+ * );
292+ * }
293+ *
294+ * function draw() {
295+ * background(200);
296+ *
297+ * // Style the text.
298+ * textAlign(CENTER);
299+ * textSize(16);
300+ *
301+ * // Display the last key pressed.
302+ * text(code, 50, 50);
303+ * }
304+ * </code>
305+ * </div>
306+ * <div>
307+ * <code>
308+ *
309+ * function setup() {
310+ * createCanvas(100, 100);
311+ * }
312+ *
313+ * function draw() {
314+ * background(220);
315+ * fill("black");
316+ * if (keyIsDown(BACKSPACE) || keyIsDown(ENTER) ||
317+ * keyIsDown(DELETE) || keyIsDown(RETURN) ||
318+ * keyIsDown(TAB) || keyIsDown(ESCAPE) ||
319+ * keyIsDown(CONTROL) || keyIsDown(OPTION) ||
320+ * keyIsDown(UP_ARROW) || keyIsDown(LEFT_ARROW) ||
321+ * keyIsDown(RIGHT_ARROW) || keyIsDown(DOWN_ARROW) ||
322+ * keyIsDown(SHIFT)) {
323+ * fill("red");
324+ * text("System Variable", 7, 75);
325+ * }
326+ *
327+ * text(key, 30, 25);
328+ * text(keyCode, 7, 25);
329+ * text(code || " ", 30, 50);
330+ * }
331+ * </div>
332+ * </code>
333+ * <div>
334+ * <code>
335+ * // Click on the canvas to begin detecting key presses.
336+ *
337+ * let x = 50;
338+ * let y = 50;
339+ *
340+ * function setup() {
341+ * createCanvas(100, 100);
342+ *
343+ * background(200);
344+ *
345+ * describe(
346+ * '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.'
347+ * );
348+ * }
349+ *
350+ * function draw() {
351+ * // Update x and y if an arrow key is pressed.
352+ * if (keyIsPressed){
353+ * if (keyIsDown(LEFT_ARROW)){
354+ * x -= 1;
355+ * }
356+ *
357+ * if (keyIsDown(RIGHT_ARROW)) {
358+ * x += 1;
359+ * }
360+ *
361+ * if (keyIsDown(UP_ARROW)) {
362+ * y -= 1;
363+ * }
364+ *
365+ * if (keyIsDown(DOWN_ARROW)) {
366+ * y += 1;
367+ * }
368+ * }
369+ *
370+ * // Style the circle.
371+ * fill(0);
372+ *
373+ * // Draw the circle.
374+ * circle(x, y, 5);
375+ * }
376+ *
377+ * </code>
378+ * </div>
379+ */
380+ fn . code = '' ;
381+
198382 /**
199383 * A `Number` system variable that contains the code of the last key pressed.
200384 *
@@ -652,7 +836,7 @@ function keyboard(p5, fn){
652836 if ( ! this . _areDownKeys ( ) ) {
653837 this . keyIsPressed = false ;
654838 this . key = '' ;
655- this . code = null ;
839+ this . code = '' ;
656840 } else {
657841 // If other keys are still pressed, update code to the last pressed key
658842 const lastPressedCode = Object . keys ( this . _downKeyCodes ) . pop ( ) ;
0 commit comments