Keyboard interaction allows users to manipulate or control the application using their keyboard. p5.js has built-in functions that allow for such interactions.
p5.js has a built-in keyPressed()
function, which is called whenever any keyboard key is pressed. We can then access the global key
variable, which stores which key is pressed.
In this example, we can see a dash in the output. To interact with it, click on the screen and press any key. The pressed key will show up on the output.
Lines 1–6: The setup()
function is called once before the application runs. This function sets up the application canvas with the size and the text properties such as size and alignment.
Lines 8–14: The showKey
variable stores which key we are displaying at the moment. The draw()
function is called once every frame. In this function the background is being reset, and the text is being displayed.
From lines 16–20: The keyPressed()
function is called once whenever any key is pressed. In this function, the showKey
variable is updated.
In this example, we can see an ellipse in the output. To interact with it, click on the screen and use "a" and "d" to move left and right, and use "w" and "s" for vertical movement.
Lines 1–7: The code begins by declaring several variables:
x
and y
: These variables store the current position of the circle on the canvas.
moveSpeed
: This variable defines the rate at which the circle moves.
movementInterval
: This variable will store the interval ID for the movement timer.
isMovingLeft
, isMovingRight
, isMovingUp
and isMovingDown
: These boolean variables track whether the respective movement key (arrow keys or WASD keys) is currently being held down.
Lines 9–13: In the setup()
function, The x
and y
variables are set to the center of the screen.
Lines 15–32: In the draw()
function, the circle is filled with a pink color using fill()
and then displayed using ellipse()
.
Lines 34–62: In the keyPressed()
function, the position x
and y
of the circle are updated, depending on what key is pressed. For example, if the key "a" or "ArrowLeft" is pressed, move the circle left.
Lines 34–62: The keyReleased()
function is triggered when a key is released. It clears the movement interval using clearInterval
to stop the continuous movement. Also, it sets all the boolean variables (isMovingLeft
, isMovingRight
, isMovingUp
, isMovingDown
) to false
, indicating that the respective movement key is no longer being held down.
p5.js allows it's users to make creative coding applications in a simple manner. Adding keyboard input to a p5.js application is intuitive and easy to implement.
Unlock your potential: p5.js fundamentals series, all in one place!
To deepen your understanding of p5.js, explore our series of Answers below:
What is p5.js?
p5.js is an open-source JavaScript library for creative coding, designed for visualizations, games, and interactive art, with a beginner-friendly approach.
How to color shapes in p5.js
Learn how to apply colors to shapes using fill()
, stroke()
, and transparency to create stunning visual effects.
How to use images in p5.js
Discover how to load, display, and manipulate images in p5.js to enhance dynamic and interactive sketches.
How to use image filters in p5.js
Enhance your images with built-in filters like grayscale, blur, and threshold to achieve unique visual effects.
How to create advanced 2D shapes in p5.js
Master the creation of complex 2D shapes using curves, vertices, and transformations for custom designs.
How to add keyboard interaction in p5.js
Learn how to detect and respond to keyboard input to add interactivity to your sketches.
How to use vectors in p5.js
Explore the power of vectors for motion, physics simulations, and dynamic interactions in creative coding.
How to add mouse interaction in p5.js
Implement mouse-based interactions such as clicks, drags, and movement tracking to enhance user engagement.
Free Resources