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 #6993

Draft
wants to merge 2 commits into
base: dev-2.0
Choose a base branch
from
Draft
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
38 changes: 37 additions & 1 deletion src/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ p5.prototype._onblur = function(e) {
* <a href="http://p5js.org/reference/#p5/keyCode">here</a>.
*
* @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
* <div><code>
Expand Down Expand Up @@ -344,6 +344,39 @@ p5.prototype._onblur = function(e) {
* </code></div>
*
* <div><code>
* 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.`);
* }
* </code></div>
*
* <div><code>
* let diameter = 50;
*
* function setup() {
Expand Down Expand Up @@ -371,6 +404,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