A function that's called once when any key is pressed.
Declaring the function keyPressed() sets a code block to run once automatically when the user presses any key:
function keyPressed() {
// Code to run.
}
The key and keyCode variables will be updated with the most recently typed value when keyPressed() is called by p5.js:
function keyPressed() {
if (key === 'c') {
// Code to run.
}
if (keyCode === ENTER) {
// Code to run.
}
}
The parameter, event, is optional. keyPressed() is always passed a KeyboardEvent object with properties that describe the key press event:
function keyPressed(event) {
// Code to run that uses the event.
console.log(event);
}
Browsers may have default behaviors attached to various key events. For example, some browsers may jump to the bottom of a web page when the SPACE key is pressed. To prevent any default behavior for this event, add return false; to the end of the function.
예제
구문
keyPressed([event])
매개변수
optional KeyboardEvent callback argument.
관련 레퍼런스
keyCode
A Number system variable that contains the code of the last key typed.
keyIsDown
Returns true if the key it’s checking is pressed and false if not.
keyIsPressed
A Boolean system variable that's true if any key is currently pressed and false if not.
keyPressed
A function that's called once when any key is pressed.