Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
p5.js ReferenceError: keyDown is not defined

Created this simple code that should move a rect in p5.js. I get the error ReferenceError: keyDown is not defined. What's wrong? Do I have to install any other libraries or is it a syntax error?

function setup() {
    createCanvas(400, 400);
}
 
let x = 0;
let y = 0;

function draw() {
    if (keyDown(68)) // d
    {
        x += 3
    }
    if (keyDown(65)) // a 
    {
        x -= 3
    }
    if (keyDown(87)) 
    {
        y -= 3
    }
    if (keyDown(83)) 
    {
        y += 3
    }

    background(220)
    rect(x, y, 30, 50);
           
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Syntax error. The correct way to check for a keypress is KeyIsDown(keycode):

function setup() {
    createCanvas(400, 400);
}
 
let x = 0;
let y = 0;

function draw() {
    if (keyIsDown(68)) // d
    {
        x += 3
    }
    if (keyIsDown(65)) // a 
    {
        x -= 3
    }
    if (keyIsDown(87)) 
    {
        y -= 3
    }
    if (keyIsDown(83)) 
    {
        y += 3
    }

    background(220)
    rect(x, y, 30, 50);
           
}
about 4 years ago · Juan Pablo Isaza Report

0

Solved by @Matei Piele with keyIsDown(), posting this so other people know, can also be solved with keyIsPressed().

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!