-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Replaced deprecated keyCode functionality and docs with KeyboardEvent.code & KeyboardEvent.key also updates the keyIsDown function to accept alphanumerics as parameters #7472
Open
Vaivaswat2244
wants to merge
6
commits into
processing:dev-2.0
Choose a base branch
from
Vaivaswat2244:dev-2.0
base: dev-2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd458c0
changed keyboard.js and added the updated objects in costants.js file
Vaivaswat2244 bcbed0f
fixed the repeat key issue in onkeyup
Vaivaswat2244 c42bb7e
updated keyIsDown() to work with characters as arguments
Vaivaswat2244 5709aac
fixed dillema of code/key and added isCode()
Vaivaswat2244 0ce4e89
added tests for isCode and small changes
Vaivaswat2244 9cfef5a
Merge branch 'dev-2.0' into dev-2.0
Vaivaswat2244 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,17 @@ | |
* @for p5 | ||
* @requires core | ||
*/ | ||
export function isCode(input) { | ||
if (typeof input !== 'string') { | ||
return false; | ||
} | ||
|
||
if (input.length === 1 && /[0-9]/.test(input)) { | ||
return true; | ||
} | ||
|
||
return input.length > 1; | ||
} | ||
function keyboard(p5, fn){ | ||
/** | ||
* A `Boolean` system variable that's `true` if any key is currently pressed | ||
|
@@ -95,7 +105,9 @@ function keyboard(p5, fn){ | |
* </code> | ||
* </div> | ||
*/ | ||
|
||
fn.keyIsPressed = false; | ||
fn.code = null; | ||
|
||
/** | ||
* A `String` system variable that contains the value of the last key typed. | ||
|
@@ -439,14 +451,15 @@ function keyboard(p5, fn){ | |
* </div> | ||
*/ | ||
fn._onkeydown = function(e) { | ||
if (this._downKeys[e.which]) { | ||
// prevent multiple firings | ||
if (this._downKeys[e.code]) { | ||
return; | ||
} | ||
this.keyIsPressed = true; | ||
this.keyCode = e.which; | ||
this._downKeys[e.which] = true; | ||
this.key = e.key || String.fromCharCode(e.which) || e.which; | ||
this.key = e.key; | ||
this.code = e.code; | ||
this._downKeyCodes[e.code] = true; | ||
this._downKeys[e.key] = true; | ||
const context = this._isGlobal ? window : this; | ||
if (typeof context.keyPressed === 'function' && !e.charCode) { | ||
const executeDefault = context.keyPressed(e); | ||
|
@@ -612,17 +625,20 @@ function keyboard(p5, fn){ | |
* </div> | ||
*/ | ||
fn._onkeyup = function(e) { | ||
this._downKeys[e.which] = false; | ||
delete this._downKeyCodes[e.code]; | ||
delete this._downKeys[e.key]; | ||
|
||
|
||
if (!this._areDownKeys()) { | ||
this.keyIsPressed = false; | ||
this.key = ''; | ||
this.code = null; | ||
} else { | ||
// If other keys are still pressed, update code to the last pressed key | ||
const lastPressedKey = Object.keys(this._downKeys).pop(); | ||
this.code = lastPressedKey; | ||
} | ||
|
||
this._lastKeyCodeTyped = null; | ||
|
||
this.key = e.key || String.fromCharCode(e.which) || e.which; | ||
this.keyCode = e.which; | ||
|
||
const context = this._isGlobal ? window : this; | ||
if (typeof context.keyReleased === 'function') { | ||
const executeDefault = context.keyReleased(e); | ||
|
@@ -807,7 +823,7 @@ function keyboard(p5, fn){ | |
* <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 | ||
|
@@ -899,11 +915,23 @@ function keyboard(p5, fn){ | |
* </code> | ||
* </div> | ||
*/ | ||
fn.keyIsDown = function(code) { | ||
// p5._validateParameters('keyIsDown', arguments); | ||
return this._downKeys[code] || false; | ||
}; | ||
function isCode(input) { | ||
if (typeof input !== 'string') { | ||
return false; | ||
} | ||
if (input.length === 1 && /[0-9]/.test(input)) { | ||
return true; | ||
} | ||
return input.length > 1; | ||
} | ||
|
||
fn.keyIsDown = function(input) { | ||
if (isCode(input)) { | ||
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. nice! |
||
return this._downKeyCodes[input] || this._downKeys[input] || false; | ||
} else { | ||
return this._downKeys[input] || this._downKeyCodes[input] || false; | ||
} | ||
} | ||
/** | ||
* The _areDownKeys function returns a boolean true if any keys pressed | ||
* and a false if no keys are currently pressed. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there a reason why the typedef is both of these while the constant is just one?