Making keyPressed() doesn't seem to work in p5.js as it did in Java, how can I make it work or replace it?

Hello !

I have a game that I created years ago as a school project in processing Java, and now I’ve been trying to port it to js so that I can put it on a webpage. I am not experienced in JS.

I did a lot of it with available resources online, I think I’ve got most of the simple formatting differences out of the way, and the code now properly displays the environment, my dinosaur and the text at the beginning of the game. Even the “press enter to start” works correctly, but then… My triceratops cannot move when I press RIGHT or LEFT.

I can’t seem to find the problem, the code seems rather straightforward in this regard…

function keyPressed() {
if (keyCode== ENTER) {
if (!gameOn) {
gameOn=true;
dead=false;
meteorY=0;
angle=HALF_PI;
mohammed.jumpHeight=0;
offTime=millis()/1000;
meteorNum=1;
}
}

if (!dead) {
if (keyCode==RIGHT) {
right=true;
mohammed.run();
}
if (keyCode==LEFT) {
right=false;
mohammed.run();
}
}

}


Mohammed is an object of the Triceratops class. I’m not sure why I named him that, maybe inspired by the Raptor Jesus meme. Anyway, Mohammed the Triceratops is the hero of this game, he has to escape the falling meteors. Run is a method of this class. Because he has to be facing whichever way he is running, the boolean “right” is used for his make() method, and also define which way the “run” method makes him go.

Here is my full code:

The Java version (which already works).

The Js version, for which I am asking for help.

The browser console doesn’t give me any error, just warnings about orientation and position captors not being available anymore, but I don’t think I need those ?

The browser in question is waterfox.

However, I also tried testing it in qutebrowser, and I found out in there not even pressing Enter to start the game works.

Looking around, I did see some pages about Javascript, I see there are a lot of different methods for capturing keyboard events, but I can’t find which ones work with Processing…

Thank you in advance, any advice would be greatly appreciated.