From d1e6050971a5999ac5183e4cc566e08674d94d13 Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Fri, 26 Apr 2024 16:25:38 -0400 Subject: [PATCH 1/2] updated keyIsDown to work with strings of alphanums --- src/events/keyboard.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/events/keyboard.js b/src/events/keyboard.js index e2e7db89d8..3659306524 100644 --- a/src/events/keyboard.js +++ b/src/events/keyboard.js @@ -307,7 +307,7 @@ p5.prototype._onblur = function(e) { * here. * * @method keyIsDown - * @param {Number} code The key to check for. + * @param {Number|String} code The key to check for. * @return {Boolean} whether key is down or not * @example *
@@ -371,6 +371,9 @@ p5.prototype._onblur = function(e) { */ p5.prototype.keyIsDown = function(code) { p5._validateParameters('keyIsDown', arguments); + if (typeof code === 'string' && code.length === 1) { + code = code.toUpperCase().charCodeAt(0); + } return this._downKeys[code] || false; }; From 1e5e85c889309c3bed04b45a9ac2ebbe40a8d7ac Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Fri, 26 Apr 2024 16:42:19 -0400 Subject: [PATCH 2/2] Update inline documentation --- src/events/keyboard.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/events/keyboard.js b/src/events/keyboard.js index 3659306524..2da1bba926 100644 --- a/src/events/keyboard.js +++ b/src/events/keyboard.js @@ -344,6 +344,39 @@ p5.prototype._onblur = function(e) { *
* *
+ * let x = 100; + * let y = 100; + * + * function setup() { + * createCanvas(512, 512); + * fill(255, 0, 0); + * } + * + * function draw() { + * if (keyIsDown('A')) { + * x -= 5; + * } + * + * if (keyIsDown('D')) { + * x += 5; + * } + * + * if (keyIsDown('W')) { + * y -= 5; + * } + * + * if (keyIsDown('S')) { + * y += 5; + * } + * + * clear(); + * ellipse(x, y, 50, 50); + * describe(`50-by-50 red ellipse moves left, right, up, and + * down with 'W', 'A', 'S', and 'D' pressed.`); + * } + *
+ * + *
* let diameter = 50; * * function setup() {