Capture keyboard only in canvas


Tried to dig through the forum but did not find an answer.

I've made a site with p5js script shown as example and an editor to create some scripts (with teaching in mind). In the p5js script I've made a function

function keyPressed() {

... what is to be done ...

return false;


The return false is added in order for the browser not to capture the keypress.

But the key pressed are captured for the whole browser. So my p5js script works, but my editor is broken :)

Is there any way to ask the pressed key to be captured ONLY if the canvas is focused and not always ?

Problem is the same with having multiples P5JS instance with keyPressed function.

TIA for your answers


Sign In or Register to comment.