Skip to content
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

Expand keyIsDown() to work with characters as arguments #6983

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,12 @@ p5.prototype._onblur = function(e) {
*
* `keyIsDown()` can check for key presses using
* <a href="#/p5/keyCode">keyCode</a> values, as in `keyIsDown(37)` or
* `keyIsDown(LEFT_ARROW)`. Key codes can be found on websites such as
* `keyIsDown(LEFT_ARROW)` or an alphanumeric string value `keyIsDown('A')` or `keyIsDown('a')`
* Key codes can be found on websites such as
* <a href="https://keycode.info" target="_blank">keycode.info</a>.
*
* @method keyIsDown
* @param {Number} code key to check.
* @param {Number|String} code key to check.
* @return {Boolean} whether the key is down or not.
*
* @example
Expand Down Expand Up @@ -872,6 +873,50 @@ p5.prototype._onblur = function(e) {
* background(200);
*
* describe(
* 'A gray square with a black circle at its center. The circle moves when the user presses "W", "A", "S", or "D". It leaves a trail as it moves.'
* );
* }
*
* function draw() {
* // Update x and y if "W", "A", "S", or "D" is pressed.
* if (keyIsDown('A') === true) {
* x -= 1;
* }
*
* if (keyIsDown('D') === true) {
* x += 1;
* }
*
* if (keyIsDown('W') === true) {
* y -= 1;
* }
*
* if (keyIsDown('S') === true) {
* y += 1;
* }
*
* // Style the circle.
* fill(0);
*
* // Draw the circle.
* circle(x, y, 5);
* }
* </code>
* </div>
*
* <div>
* <code>
* // Click on the canvas to begin detecting key presses.
*
* let x = 50;
* let y = 50;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* describe(
* '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.'
* );
* }
Expand Down Expand Up @@ -905,6 +950,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;
};

Expand Down
4 changes: 4 additions & 0 deletions test/unit/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,9 @@ suite('Keyboard Events', function() {
test('keyIsDown should return false if key is not down', function() {
assert.strictEqual(myp5.keyIsDown(35), false);
});

test('keyIsDown should return false if key has length more than 1', function() {
assert.strictEqual(myp5.keyIsDown('ab'), false);
});
});
});